[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 ...
随机推荐
- Redis主从复制以及主从复制原理
Redis 是一个开源的使用 ANSI C 语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value 数据库,并提供多种语言的 API.从 2010年 3 月 15 日起,Redis 的开 ...
- 直击JDD | 京东技术全景图首次展示 四大重磅智能技术驱动产业未来!
11月19日,主题为"突破与裂变"的2019京东全球科技探索者大会(JDDiscovery)在京盛大开幕,京东集团展示了完整的技术布局与先进而丰富的对外技术服务,对外明确诠释了&q ...
- 存储基本概念(lun,volume,HBA,DAS,NAS,SAN,iSCSI,IPSAN)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/liukuan73/article/det ...
- token获取在controller层中
集合判断是否为空 注意:token获取在controller层中,token中存的所有数据都要在controller中获取 在自己的接口里调用别的接口需要判断一下返回值是否为空
- 【按位dp】文盲的学习方法
当年大神的文章 <浅谈数位统计问题> 对于没什么文化(x 没有充分时间或懒得看那么多理论 应付个水考试的我 eg:62问题 某大大的代码和分析 #include <iostream& ...
- ios uiimagepickercontroller 选择相册或者拍照上传
首先需要实现UIImagePickerControllerDelegate 代理 实现其imagePickerController 方法 这里用于选择图片或的拍照回调 //调用相机拍照 或者 图库选 ...
- 移动端web前端开发
移动端浏览器现状 视口 meta视口标签 二倍图 移动端主流方案 移动端技术解决方案 移动端常见布局 1.流式布局(百分比布局) 2.flex布局 3.rem适配布局 1)rem单位 2)媒体查询 3 ...
- linux笔记(一)——基本命令和快捷键
linux笔记(一) 1.常用BASH快捷键 编辑命令 快捷键 作用 Ctrl + a 移到命令行首 Ctrl + e 移到命令行尾 Ctrl + xx 在命令行首和光标之间移动 Ctrl + u 从 ...
- dubbo配置文件加载顺序
JVM 启动 -D 参数优先,这样可以使用户在部署和启动时进行参数重写,比如在启动时需改变协议的端口. XML 次之,如果在 XML 中有配置,则 dubbo.properties 中的相应配置项无效 ...
- PTC【Creo OR Proe】添加参数的方法
Dim model As IpfcModel Try model = CoCreoAsyncConnection.Session.CurrentModel If model Is Nothing Th ...