[LC] 222. Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes.
Note:
Definition of a complete binary tree from Wikipedia:
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
Example:
Input:
1
/ \
2 3
/ \ /
4 5 6 Output: 6
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int countNodes(TreeNode root) {
int left = leftCount(root);
int right = rightCount(root);
if (left != right) {
return 1 + countNodes(root.left) + countNodes(root.right);
} else {
return (1 << left) - 1;
}
} private int leftCount(TreeNode cur) {
int res = 0;
while (cur != null) {
res += 1;
cur = cur.left;
}
return res;
} private int rightCount(TreeNode cur) {
int res = 0;
while (cur != null) {
res += 1;
cur = cur.right;
}
return res;
}
}
[LC] 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】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 ...
- [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- Java for LeetCode 222 Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- 222. Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- (medium)LeetCode 222.Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- LeetCode OJ 222. Count Complete Tree Nodes
Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium Given a complete binary tree, cou ...
- 【Leetcode】222. Count Complete Tree Nodes
Question: Given a complete binary tree, count the number of nodes. Definition of a complete binary t ...
随机推荐
- 《C Primer Plus》- 第一章 初试C语言
本笔记写于2020年1月25日. 从今天开始,我要全面的.彻底的将未来计划中所有的知识重新规划学习一遍,并整理成一套全面的笔记体系.为我将来的职业打下坚实的基础.而所有的一切从C语言开始. 本系列文章 ...
- CocoaPods为多个target添加依赖库/Podfile的配置
Podfile的相关配置,请看官方文档http://guides.cocoapods.org/syntax/podfile.html 1)多个target公用相同库,还可以添加额外的不同第三方库 编辑 ...
- VUE获取焦点
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 【Pytyon模块】logging模块-日志处理
一.日志相关概念 1.日志的作用 通过log的分析,可以方便用户了解系统或软件.应用的运行情况:如果你的应用log足够丰富,也可以分析以往用户的操作行为.类型喜好.地域分布或其他更多信息:如果一个应用 ...
- 批量导出数据库表(oracle)
批量导出数据库表(oracle) 要求:导出sql文件,包含表结构和数据. 方案一 1:用cmd进入命令行输入:tnsping cmstar就是测试172.18.13.200是否连接成功2:导入与导出 ...
- YouTube推出慈善组合工具,能引国内视频网站跟风吗?
互联网的出现不仅仅让大众的工作和生活更便利,更深度改变着传统事物的形态,让其被更多人广泛地认知并接触到.如,原本在线下通过彩页.手册.横幅等进行宣传.募捐的慈善,就通过互联网展现出更为强大的影响力.而 ...
- 使用sklearn做特征工程
1 特征工程是什么? 有这么一句话在业界广泛流传:数据和特征决定了机器学习的上限,而模型和算法只是逼近这个上限而已.那特征工程到底是什么呢?顾名思义,其本质是一项工程活动,目的是最大限度地从原始数据中 ...
- Jisa's Notebook
同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一下本文的上下文. 本文讨论的背景是Linux环境下的network IO. ...
- PAT Basic 1013 数素数 (20) [数学问题-素数]
题目 令Pi表示第i个素数.现任给两个正整数M <= N <= 10^4,请输出PM到PN的所有素数. 输⼊格式: 输⼊在⼀⾏中给出M和N,其间以空格分隔. 输出格式: 输出从PM到PN的 ...
- PAT Basic 1034 有理数四则运算(20) [数学问题-分数的四则运算]
题目 本题要求编写程序,计算2个有理数的和.差.积.商. 输⼊格式: 输⼊在⼀⾏中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分⼦和分⺟全是整型范围内的整数, ...