Count Complete Tree Nodes ——LeetCode
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 2hnodes inclusive at the last level h.
题目大意:给定一个完全二叉树,返回这个二叉树的节点数量。
解题思路:一开始想着层次遍历,O(n),结果TLE,后来优化了下,采用递归的方式来做,总节点数等于1+左子树的数量+右子树的数量,因为是完全二叉树,所以对于子树是满二叉树的可以直接计算出结果并返回。
public class Solution {
public int countNodes(TreeNode root) {
if(root==null){
return 0;
}
TreeNode left =root,right=root;
int cnt = 0;
while(right!=null){
left=left.left;
right=right.right;
cnt++;
}
if(left==null){
return (1<<cnt)-1;
}
return 1+countNodes(root.left)+countNodes(root.right);
}
}
Count Complete Tree Nodes ——LeetCode的更多相关文章
- Count Complete Tree Nodes || LeetCode
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * s ...
- 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 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- 【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 ...
随机推荐
- Java异常之try,catch,finally,throw,throws
Java异常之try,catch,finally,throw,throws 你能区分异常和错误吗? 我们每天上班,正常情况下可能30分钟就能到达.但是由于车多,人多,道路拥挤,致使我们要花费更多地时间 ...
- 把nc v6的源码看懂
看懂nc v6的源码! 碧桂园全部的正式环境的补丁都在我手里. 2015-11-18 2:33 谢谢两位一起努力的兄弟 谢谢超哥,谢谢祈冰哥,谢谢连老师,陈明大哥,谢谢龙哥,珍玉,谢谢廖生哥,谢谢林春 ...
- ORACLE解锁数据库用户
the account is locked解决办法: 1.进入sqlplus sqlplus "/as sysdba" 2.解锁: alter user hpmng account ...
- git代码库的使用
代码库/使用指南 http://learn.zone.jd.com/cmsuser/index.htm 在win7系统下使用TortoiseGit(乌龟git)简单操作Git@OSC http://m ...
- 获取Host文件权限 注册表导入
Windows Registry Editor Version 5.00 ;取得文件修改权限 [HKEY_CLASSES_ROOT\*\shell\runas] @="管理员权限" ...
- style currentStyle getComputedStyle的区别和用法
先介绍下层叠样式表的三种形式: 1.内联样式,在html标签中style属性设置. <p style="color:#f90">内联样式</p> 2.嵌入样 ...
- 【HDU4010】【LCT】Query on The Trees
Problem Description We have met so many problems on the tree, so today we will have a query problem ...
- window下配置SSH连接GitHub、GitHub配置ssh key(转)
转自:http://jingyan.baidu.com/article/a65957f4e91ccf24e77f9b11.html 此经验分两部分: 第一部分介绍:在windows下通过msysGit ...
- 2 - Annotations标注
下面是TestNG标注和参数的一个快速预览 @BeforeSuite 被标注的方法会在这个套件的所有测试执行之前执行 @AfterSuite 被标注的方法会在这个套件的所有测试执行之后执行 @Bef ...
- 极简主义,对逻辑操作符||和&&深度运用的理解
丧心病狂的代码压缩好像违背了愉快阅读的本真,所以刨根问底想要了解个究竟,最后发现这不只是前端大牛装逼卖萌的绝技,也是代码洁癖爱好者的利器,毕竟这样看上去确实蛮优雅的; 一开始,我们接触到的代码是这样的 ...