LeetCode——Count Complete Tree Nodes
Description:
Given a complete binary tree, count the number of nodes.
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.
思路:给一颗完全二叉树求其节点的个数。首先要明确完全二叉树的定义:把满二叉树从上到下,从左到右进行编号,完全二叉树是其中编号没有断续的部分。也就是说完全二叉树只可能在最右子树的叶子节点的位子上有空缺。而且左右子树的高度差不能大于1。所以只要是count(左节点)==count(右节点)那么就是一颗满二叉树。可以用公式计算出节点的个数NodeCount=2^h - 1。若不是满二叉树的话就递归遍历求count(左子树) + count(右子树) + 1。
/**
* 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) {
if(root==null) return 0;
TreeNode left = root, right = root;
int leftCount = 0;
while(left!= null) {
left = left.left;
leftCount ++;
}
int rightCount = 0;
while(right != null) {
right = right.right;
rightCount ++;
}
if(leftCount==rightCount) {
return (2<<(leftCount-1)) - 1;
} else {
return countNodes(root.left) + countNodes(root.right) + 1;
}
} }
LeetCode——Count Complete Tree Nodes的更多相关文章
- 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面试准备: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] 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 ...
随机推荐
- inux内存映射和共享内存理解和区别
可以看到内存映射中需要的一个参数是int fd(文件的标识符),可见函数是通过fd将文件内容映射到一个内存空间, 我需要创建另一个映射来得到文件内容并统计或修改,这时我创建这另一个映射用的仍是mmap ...
- SpringBoot配置成Liunx服务
spring boot 可以打包成可执行的脚本来启动,其原理是在打成包时,将shell脚本注入到jar包中 #参考:https://docs.spring.io/spring-boot/docs/1. ...
- 数据导入报错 Got a packet bigger than‘max_allowed_packet’bytes
数据导入报错:Got a packet bigger than‘max_allowed_packet’bytes的问题 2个解决方法: 1.临时修改:mysql>set global max_a ...
- Windows消息目录
Windows消息目录1. WM_NULL=$0000:2. WM_CREATE=$0001: 应用程序创建一个窗口3. WM_DESTROY=$0002: 一个窗口被销毁4. WM_MOVE=$00 ...
- ADT下载地址(含各版本)
2015/05/07 新增 ADT-23.0.6.zip2015/01/18 新增(未测试,不知下载过程中是否有问题,请网友自行测试,最好能把测试结果告知,谢谢)ADT-23.0.3.zipADT ...
- 关于Cocos2d-x节点和精灵节点的坐标、位置以及大小的设置
1.cocos2d-X中的坐标(0,0),就是运行框的左下角位置,所以运行框看起来就是一个第一象限. 2.节点的锚点就是我们setPosition所设定的位置,默认锚点是在节点的中心,也就是setPo ...
- selenium测试(Java)-- 验证信息(八)
package com.test.validationinfor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.f ...
- Python之批量改变图片大小
image_pylib模块:https://github.com/huangshiyu13/image_pylib data_engine模块:https://github.com/huangshiy ...
- numpy得到数组的index
itemindex = numpy.where(array==item)
- C++ 类的复制控制
写了又删,删了又写,才发现这一章节不好描述. 那就假定个前提吧,假定已经知道: ① C++的类有构造函数. ② 如果不提供任何构造函数,那编译器会生成默认的无参构造函数--默认构造函数只会进行成员变量 ...