笔试题:求二叉树叶子节点的个数

来源:互联网 发布:阿里挂载数据盘 编辑:程序博客网 时间:2024/04/27 14:24
1.递归处理,叶子节点的个数为1;
然后还有O(2)=O(0)-1;
度为2的节点比度为0的节点少一个
int BinaryTreeCount(TreeNode* root){int nodes=0;if(root==NULL)return 0;else if(root->left==NULL &&root->right=NULL)return 1;else nodes=BinaryTreeCount(root->left)+BinartTreeCount(root->right);return nodes;}

阅读全文
0 0
原创粉丝点击