【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 ...
随机推荐
- Most efficient way to get the last element of a stream
Do a reduction that simply returns the current value: Stream<T> stream; T last = stream.reduce ...
- ZW云推客即将登场
ZW云推客即将登场 即"4K云字库"框架文件发布后,ZW团队即将发布一全功能的:ZW云推客系统. ZW云推客系统,是原zBrow"百万社区营销系统".&qu ...
- cocos2dx 3.x 蒙板 遮罩 点击圆功能
//注册触摸 EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create(); listener->onT ...
- MySQL基准测试工具--sysbench
我们需要知道的是sysbench并不是一个压力测试工具,是一个基准测试工具.linux自带的版本比较低,我们需要自己安装sysbench. [root@test2 ~]# sysbench --ver ...
- Java生成PDF之iTextPDF的使用
今天做财务方面相关数据的导出功能,需要导出PDF和Excel,在项目经理那里得知有一个叫iTextPDF的java框架导出PDF文件很好用,于是拿来玩儿玩儿. package com.smart.pr ...
- Ubuntu安装 Spark2.3.0 报错原因及解决
Ubuntu 安装Spark出现的问题及解决 最近在搭建Hadoop集群环境和Spark集群环境,出现的问题可能不太复杂,纯粹记录安装步骤和问题解决办法.集群环境使用的是(2台)阿里云主机,操作系统是 ...
- Android 开机Process xxx (pid xxxx) has died问题分析
系统中有一个监听BOOT_COMPLETED广播的自启应用,概率性出现启动后被kill掉的现象.Log如下: - :: I ActivityManager: Process com.test.xxx ...
- [CF600E]Lomsat gelral
题意翻译 一棵树有n个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和. 线段树合并板子题,没啥难度,注意开long long 不过这题$dsu$ $on$ $tre ...
- 记第一场atcoder和codeforces 2018-2019 ICPC, NEERC, Northern Eurasia Finals Online Mirror
下午连着两场比赛,爽. 首先是codeforses,我和一位dalao一起打的,结果考炸了,幸亏不计rating.. A Alice the Fan 这个就是记忆化搜索一下预处理,然后直接回答询问好了 ...
- 记一次Servlet中getAttribute的错误.
package com.ykmimi.order.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher ...