[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 ...
随机推荐
- C++-codeblocks安装
2020-02-15 "Test_leetcode - Debug": The compiler's setup (GNU GCC Compiler) is invalid, so ...
- 网络拓扑实例10:MSTP+VRRP组合组网
组网图形 MSTP+VRRP组合简介 网络中部署VRRP负载分担时,多台设备同时承担业务,每个虚拟设备都包括一个Master设备和若干个Backup设备.如果为了接入备份需要同时部署冗余链路,则需要部 ...
- docker数据持久化/共享---volume,bind-mount,tmpfs-mount
一.将Docker数据挂载到容器 在Docker中,要想实现数据的持久化(所谓Docker的数据持久化即数据不随着Container的结束而结束),需要将数据从宿主机挂载到容器中.目前Docker提供 ...
- CSDN-markdown编辑器使用方法
这里写自定义目录标题 欢迎使用Markdown编辑器 新的改变 功能快捷键 合理的创建标题,有助于目录的生成 如何改变文本的样式 插入链接与图片 如何插入一段漂亮的代码片 生成一个适合你的列表 创建一 ...
- PyQt(Python+Qt)学习随笔:QStandardItemModel指定行和列创建模型后的数据项初始化的两种方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QStandardItemModel通过构造方法 QStandardItemModel(int ro ...
- python 保存list,map方法
1. 保存list import numpy as np a = [1,2,3,4,5] np.save("number.npy", a) k = np.load("nu ...
- 移动端点击300ms延迟问题
移动端点击延迟事件 1. 移动端浏览器在派发点击事件的时候,通常会出现300ms左右的延迟 2. 原因: 移动端的双击会缩放导致click判断延迟 解决方式 1. 禁用缩放 `<meta nam ...
- 【题解】「AT4303」[ABC119D] Lazy Faith
AT4303 [ABC119D] Lazy Faith[题解][二分] AT4303 translation 有 \(a\) 个点 \(s\),有 \(b\) 个点 \(t\),问从点 \(x\) 出 ...
- JOISC2020 自闭记
以下是我考场上的思路,很多题都不是正解.对于某些题目,我们使用<代码部落>中的题解,希望大家能够看懂 JOISC2020 Round1 自闭记 T1 11 pts 算法:考虑\(DP\). ...
- 【NOI2020】美食家(矩阵)
Description 给定一张有向图,\(n\) 个顶点,\(m\) 条边.第 \(i\) 条边从 \(u_i\) 到 \(v_i\),走完该边的用时为 \(w_i\).每一个点有一个价值 \(c\ ...