文章
1
粉丝
0
获赞
0
访问
424
(1)创建一个数组,将整颗树的中序遍历序列存入数组,再创建一个数组,将结点是否存在左右子树的状态存入数组。遍历两个数组,维护一个count变量,最后返回count。
(2)int countNodes(TreeNode*root){
if(root==null){return 0;}
TreeNode*P=root;
int ans[n]=-1;
int i = 0;
int flag[n] = 0;
int count = 0;
void dfs(TreeNode*P){
dfs(P→lchild);
visit(P);
if(P!=null){ans[i++]=P→val;}
if(P→lchild==null||P→rchild==null){ &nbs...
登录后发布评论
暂无评论,来抢沙发