Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes.
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.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int countNodes(TreeNode root) { int leftDepth = leftDepth(root);
int rightDepth = rightDepth(root); if (leftDepth == rightDepth) {
return ( << leftDepth) - ;
}
return + countNodes(root.left) + countNodes(root.right); } private int rightDepth(TreeNode root) {
int depth = ;
while (root != null) {
root = root.right;
depth++;
}
return depth;
} private int leftDepth(TreeNode root) {
int depth = ;
while (root != null) {
root = root.left;
depth++;
}
return depth;
}
}
Count Complete Tree Nodes的更多相关文章
- leetcode面试准备:Count Complete Tree Nodes
1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...
- leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- 完全二叉树的节点个数 Count Complete Tree Nodes
2018-09-25 16:36:25 问题描述: 问题求解: 单纯遍历了一遍,emmm,果然TLE. 解题思路就是比较左边树高度和右边树高度,如果相等,那么就是一个满二叉树,返回1 << ...
- 【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 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 ...
- 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 ...
- leetcode_222 Count Complete Tree Nodes
题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree fr ...
随机推荐
- eclipse的几个快捷键
Alt / 自动补全 Ctrl Shift F 整理代码 Ctrl / 注释或取消注释 Ctrl 鼠标移到变量或类名上 转到声明/实现的接口/返回类型等 Ctrl Shift O import所有缺失 ...
- linux 代码分析工具 gprof - 以wpa_supplicant为例
当我们遇到一个新的程序的时候,经常会无从下手,需要debug一个功能的时候,我们不知道函数的运行流程是怎么样的,这就需要借助工具来帮助我们加快流程了.这里以分析wpa_supplicant为例 ...
- Spark环境搭建遇到的问题
1.始终找不到Hive表的问题 原因:官方编译后的Spark1.2.0+hadoop2.4与hadoop2.4.1不匹配 解决方法有两个: a.将Hadoop2.4.1替换为2.4.0版本 b.重新编 ...
- RabbitMQ消息队列在PHP下的应用
消息队列的实现中,RabbitMQ以其健壮和可靠见长.公司的项目中选择了它作为消息队列的实现.关于MQ的机制和原理网上有很多文章可以看,这里就不再赘述,只讲几个比较容易混淆的问题 1,binding ...
- socket编程知识
Socket语法及相关: Socket Familile(地址簇) socket.AF_UNIX unix本机进程间通信 socket.AF_INET IPV4 经常用的还是这里 so ...
- RGW 负载均衡和高可用的几个方案对比
注:在RGW网关主机网卡已经是10Gb的情况下,如下三个方案没有引入LVS:如果RGW是Gb网卡,可以考虑引入LVS做流量负载均衡. RGW部署采用 fastcgi+nginx 扩展性和可配置性更强. ...
- 解决:/bin/sh: 1: /home/**/custom_app.sh: Permission denied错误
出现如下错误,一般是执行权限不够. /bin/sh: : /home/custom_app.sh: Permission denied 解决方法是:cd 到此文件目录,对提示的文件赋予可执行权限或读写 ...
- gcc 编译时 include 搜索路径
这是一个不复杂的问题:但是网上很多回答都不全面:偶找了一个比较全面的(测试过): 引用http://blog.csdn.net/fjb2080/archive/2010/01/23/5247494.a ...
- 修改Firefox的User-Agent,伪装修改秘籍
火狐浏览器修改userAgent的办法一: 在火狐浏览器地址栏输入"about:config",按下回车进入设置菜单. 找到"general.useragent.over ...
- mybatis 批量更新
<update id="batchUpdate" parameterType="java.util.List"> <foreach colle ...