当前位置:网站首页>leetcode222、完全二叉树的节点个数
leetcode222、完全二叉树的节点个数
2022-04-22 20:22:00 【Java全栈研发大联盟】
题目描述
示例:

输入:root = [1,2,3,4,5,6]
输出:6
解法一:递归
凡是看到这种求二叉树的节点个数的问题,首先想到能不能用递归,然后如果能用递归的话,我们找出递归的逻辑。比如本题:
先确定单层循环逻辑: 先求它的左子树的节点数量,再求它的右子树的节点数量,最后取总和再加一 (加1是因为算上当前中间节点)就是目前节点为根节点的节点数量。
int leftnum=_countNodes(root->left);
int rightnum=_countNodes(root->right);
int treenum=leftnum+rightnum+1;
return treenum;
然后我们要知道递归的终止条件,对于本题而言,终止的情况就是
if(root==nullptr)
{
return 0;
}
所以很容易就能写出本题的答案代码
class Solution {
public:
int countNodes(TreeNode* root) {
if(root==nullptr)
{
return 0;
}
int left=countNodes(root->left);
int right=countNodes(root->right);
return left+right+1;
}
};
时间复杂度:O(n) 空间复杂度:O(log n)
解法二:迭代,层序遍历
因为本题的树形结构比较特殊,是完全二叉树,所以我们可以想到通过层序遍历来解决,从树的根节点开始,一层一层的往下遍历,遍历到最后一层就可以数完所有的节点个数
代码如下: (注:层序遍历的实现方式一般使用queue队列来实现)
class Solution {
public:
int countNodes(TreeNode* root) {
if(root==nullptr)
{
return 0;
}
queue<TreeNode*> q;
q.push(root);
int result=0;
while(!q.empty())
{
int size=q.size();
for(int i=0;i<size;i++)
{
TreeNode* node=q.front();
q.pop();
result++;
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
}
}
return result;
}
};
时间复杂度:O(n) 空间复杂度:O(n)
如果本题要我们计算“满二叉树”的节点个数,那就更简单。
因为满二叉树的节点个数很容易通过树的高度来计算。满二叉树的节点个数= ( 2 n − 1 ) (2^n-1) (2n−1)
代码也很好写:
int countNodes(TreeNode* root) {
int h = 0;
// 计算树的高度
while (root != null) {
root = root.left;
h++;
}
// 节点总数就是 2^h - 1
return (int)Math.pow(2, h) - 1;
}
版权声明
本文为[Java全栈研发大联盟]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_40241957/article/details/124348294
边栏推荐
- CmsEasy7.6.3.2逻辑漏洞
- Postman tests the correct posture of array, list and map input APIs
- Linux下安装Mysql 8.0 + eclipse配置详细教程
- Detailed tutorial on installing MySQL 8.0 + eclipse configuration under Linux
- Cannot proceed because system tables used by Event Scheduler were found damaged
- 时间戳转换
- Kingbasees service startup method of Jincang database
- 字符数组与字符串:删除字符串所有空格。 (10 分)编写一个函数,用来删除字符串中的所有空格。
- MySQL alter best practices summary
- Microservice architecture from 0 to 1, build the whole process record, hand-in-hand teaching, serialization
猜你喜欢
随机推荐
Boot implementation of IAP
Why do I suggest you work in a technical position instead of a clerical position, sales
分库分表&百亿级数据迁移
Microservice architecture from 0 to 1, build the whole process record, hand-in-hand teaching, serialization
Recommended chrome plug-ins
Industry trends are far more important than efforts -- top test technology summary
Thinking and summary of multi switch and multi service line design
The WiFi next door, I break every second
Security features of kingbasees
[unity / C] the game has a regional collapse and a deep international pit
sys_ctl启动kingbase单机服务时报错:could not bind IPv4 address “0.0.0.0“: Address already in use
文件上传问题记录
Operation and maintenance (33) centos7 6 deploy kubernetes cluster through kubedm
Markdown learning and Practice
判断是否发生塑性变形的条件:von Mises屈服准则
Screen adaptation of Android interview questions + Aidl
STM32 uses USB virtual serial port + ymodem to upgrade IAP
The swindler made nearly 180 million profits with AI voice. Victim: can't you tell it's a robot
Application of C language bit field -- storing eight flag bits in one byte
06. Refactoring - simplifying conditional expressions


![【无标题】LeetCode 396 旋转函数[数学 找规律] HERODING的LeetCode之路](/img/7a/c365daf5a61f02cb3cc8d7ec044283.png)




