[leetcode]222. Count Complete Tree Nodes完全二叉树的节点数
/*
满二叉树的特点是2^n-1,对于完全二叉树,一个node如果左右子树深度相同,那么
是一个满二叉树。如果不是,那就把node算上,继续往下看,下边的可能是满二叉树
由于完全二叉树中有一些子满二叉树,所以可以省时间
*/
public int countNodes(TreeNode root) {
if (root==null) return 0;
int l = getLeft(root);
int r = getRight(root);
return (l == r)?(1<<l)-1:countNodes(root.left)+countNodes(root.right)+1;
}
//获取左子树深度
private int getLeft(TreeNode root)
{
if (root==null) return 0;
return getLeft(root.left)+1;
}
//获取右子树深度
private int getRight(TreeNode root)
{
if (root==null) return 0;
return getRight(root.right)+1;
}
这个题直接遍历会超时。利用了满二叉树的特点,完全二叉树中满二叉树还是有不少的。
对于满二叉树的定义,国内的定义除了每个节点都左右子树外,要求所有叶子节点都在一层上,但是国际上的只要前一个条件就可以。这里说的满二叉树是国内定义的那种。
完全二叉树相对于满二叉树,最后一层可能缺失最右边几个节点。
以后遇见完全二叉树,可以多考虑下满二叉树的特点。
[leetcode]222. Count Complete Tree Nodes完全二叉树的节点数的更多相关文章
- [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- (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 222. Count Complete Tree Nodes
题意 给一棵 complete binary tree,数数看一共有多少个结点.做题链接 直观做法:递归 var countNodes = function(root) { if(root===nul ...
- 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 完全二叉树的节点个数
给出一个完全二叉树,求出该树的节点个数.完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底层为第 h ...
- leetcode 222.Count Complete Tree Nodes
完全二叉树是从左边开始一点点填充节点的,因此需要计算所有的节点的个数. 则分别从左边和右边来进行传递的,当左右是完全二叉树的时候,其节点个数就是pow(2,h)-1. /** * Definition ...
- 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 ...
随机推荐
- kali putty远程连接允许以root身份登录
原文链接:https://blog.csdn.net/long_long_chuang/article/details/70227874 kali linux通过ssh+putty来实现远程登录(亲测 ...
- 安装spyder记录
sudo apt-get install spyder 报错:ERROR: Could not find a version that satisfies the requirement pyqt5& ...
- Java面试专题-多线程篇(2)- 锁和线程池
- Jquery返回顶部插件
自己jquery开发的返回顶部,当时只为了自己用一下,为了方便,修改成了插件... 自己的博客现在用的也是这个插件..使用方便!! <!DOCTYPE html> <html> ...
- Object.prototype.toString.call()和typeof()区别
js中判断数据类型都知道用tyoeof(),但是他只能判断object,boolean,function,string,number,undefined还有symbol(es6新增)这几种初步类型,比 ...
- AGC039D 题解
题目描述 给定在笛卡尔坐标系的单位圆上的\(N\)个点(圆心为\((0, 0)\)).第\(i\)个点的坐标为\((cos(\frac{2 \pi T_i}{L}), sin(\frac{2 \pi ...
- Acwing 403. 平面
以一个这个环为基准,剩下的边可以放在圈外,也可以放在圈内,两种状态. 如果两条线段出现了环上意义的交叉即冲突,即不能同时放在圈外/内. 这是典型的 2-SAT 问题,因为关系传递是无向的,即逆命题与原 ...
- STL——容器(Map & multimap)的查找
map.find(key); //查找键key是否存在,若存在,返回该键的元素的迭代器:若不存在,返回map.end(); map.count(key); //返回容器中键值为key的对组个数 ...
- 00-JAVA语法基础
1. 原码为数的二进制数,反码是将其二进制数每一位按位取反.补码则不同,正数的补码是其原码本身,负数的补码是其除符号位以外其他每一位按位取反再加一,符号位不变. int a=100; a=a>& ...
- JavaSE13-常用API&异常
1.包装类 1.1 基本类型包装类 基本类型包装类的作用 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据 常用的操作之一:用于基本数据类型与字符串之间的转换 基本类型 包装 ...