【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 ...
随机推荐
- 第1章 1.3计算机网络概述--规划IP地址介绍MAC地址
IP地址的作用是:指定发送数据者和接收数据者. MAC地址的作用:指定数据包的下一跳转设备.就是说明数据下一步向谁发. 路由器的作用:在不同的网段中转发数据.路由器本质就是有2个网卡的设备. 网卡:用 ...
- Java-idea-PMD源文件级别潜在bug查看
一.概述 PMD(Project Manager Design)是一种开源分析Java代码错误的工具.与其他分析工具不同的是,PMD通过静态分析获知代码错误.也就是说,在不运行Java程序的情况下报告 ...
- 数据库知识,mysql索引原理
1:innodb底层实现原理:https://blog.csdn.net/u012978884/article/details/52416997 2:MySQL索引背后的数据结构及算法原理 ht ...
- makefile中ifeq与ifneq dev/null和dev/zero简介 dd命令
ifeq语法是ifeq "<arg1>;" "<arg2>;" ,功能是比较参数“arg1”和“arg2”的值是否相同,相同时为1 i ...
- placement new--《C++必知必会》 条款35
placement new是重载operator new的一个标准.全局的版本,它不能被自定义的版本代替(不像普通的operator new和operator delete能够被替换成用户自定义的版本 ...
- cocos代码研究(19)Widget子类ImageView学习笔记
理论基础 显示图片的小控件,继承自 Widget . 代码实践 static ImageView * create()创建一个空的ImageView static ImageView * create ...
- 机器学习实战python3 Logistic Regression
代码及数据:https://github.com/zle1992/MachineLearningInAction logistic regression 优点:计算代价不高,易于理解实现,线性模型的一 ...
- 20155307 2016-2017-2 《Java程序设计》第8周学习总结
20155307 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 日志API:使用日志的起点是Logger类,要取得Logger类,必须使用Logger的静态 ...
- 【开源】检查更新程序 CheckUpdate.Net 的实现
访问最新源代码及更新历史:http://git.oschina.net/xcong/CheckUpdate.Net DownLoad 更新历史 version 1.2 [新增]添加UpdateFile ...
- Python3.x:sys.argv[]的简介
Python3.x:sys.argv[]的简介 sys模块通过sys.argv提供对任何命令行参数的访问.主要有两个参数变量: sys.argv是命令行参数的列表. len(sys.argv)是命令行 ...