【LeetCode 222_完全二叉树_遍历】Count Complete Tree Nodes
解法一:递归
int countNodes(TreeNode* root)
{
if (root == NULL)
return ; TreeNode *pLeft = root->left;
TreeNode *pRight = root->right;
int ldepth = , rdepth = ;
while (pLeft) {
ldepth++;
pLeft = pLeft->left;
}
while (pRight) {
rdepth++;
pRight = pRight->right;
}
if (ldepth == rdepth)
return ( << (ldepth + )) - ;
else
return countNodes(root->left) + countNodes(root->right) + ;
}
解法二:迭代
int GetDepth(TreeNode *root)
{
int depth = ;
while (root) {
depth++;
root = root->left;
}
return depth;
} int countNodes(TreeNode* root)
{
if (root == NULL)
return ; int depth = GetDepth(root);
int leaf = ;
int depth_left, depth_right;
while (true) {
depth_left = GetDepth(root->left);
depth_right = GetDepth(root->right);
if (depth_left == && depth_right == ) {
leaf += ;
break;
} if (depth_left == depth_right) {
leaf += << (depth_left - );
root = root->right;
} else {
root = root->left;
}
}
return ( << (depth - )) - + leaf;
}
【LeetCode 222_完全二叉树_遍历】Count Complete Tree Nodes的更多相关文章
- leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- leetcode面试准备:Count Complete Tree Nodes
1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 【刷题-LeetCode】222. Count Complete Tree Nodes
Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Note: Definition ...
- 完全二叉树的节点个数 Count Complete Tree Nodes
2018-09-25 16:36:25 问题描述: 问题求解: 单纯遍历了一遍,emmm,果然TLE. 解题思路就是比较左边树高度和右边树高度,如果相等,那么就是一个满二叉树,返回1 << ...
- LeetCode Count Complete Tree Nodes
原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- LeetCode OJ:Count Complete Tree Nodes(完全二叉树的节点数目)
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
随机推荐
- 关于Controller层返回JSON字符串
/** * 导入jackson包. * @param pn * @return */ @RequestMapping("/emps") @ResponseBody public M ...
- (2.13)Mysql之SQL基础——触发器
(2.13)Mysql之SQL基础——触发器 关键词:Mysql触发器 1.一般形式 -- 0.查看触发器[1]SHOW TRIGGERS;[2]SELECT * FROM `information_ ...
- android 本地通知
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notificat ...
- linux 安装 mysql-5.6.26
linux安装mysql-5.6.26 查看工具:winscp 下载地址 http://mirrors.sohu.com/mysql/MySQL-5.6/ 文件: mysql-5.6.26-linux ...
- nodejs Async详解之三:集合操作
Async提供了很多针对集合的函数,可以简化我们对集合进行异步操作时的步骤.如下: forEach:对集合中每个元素进行异步操作 map:对集合中的每个元素通过异步操作得到另一个值,得到新的集合 fi ...
- PHP debug_backtrace() 函数打印调用处的调试信息
http://php.net/manual/zh/function.debug-backtrace.php debug_backtrace (PHP 4 >= 4.3.0, PHP 5, PHP ...
- selenium自我手册
(转载需注明原文地址和作者两项,否则视为非授权) 语言:python 0x00 预热 下载安装包 pip install selenium 确定所用的浏览器 支持firefox,chrome,IE,e ...
- Linux Find命令使用方法举例
linux命令之find命令使用举例. 在当前目录和子目录下查找文件MyCProgram.c # find . -name "MyCProgram.c" 查找文件且忽略大小写 ...
- AVAudioFoundation(1):使用 AVAsset
本文转自:AVAudioFoundation(1):使用 AVAsset | www.samirchen.com 本文主要内容来自 AVFoundation Programming Guide. 要了 ...
- jmeter导入csv压测
压测csv数据源(设置为utf-8格式),jmeter不需要第一行参数名 新建一个线程组,根据压测数据调整设置需要的循环次数(测试数据有9行,设置循环次数为9) 添加http头信息 Content-T ...