【Leetcode】222. Count Complete Tree Nodes
Question:
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.
Tips:
给定一个完全二叉树,求树中结点的个数。
思路:
完全二叉树的特性:完全二叉树:叶节点只能出现在最下层和次下层,并且最下面一层的结点都集中在该层最左边的若干位置的二叉树。即除了最后一层,前面所有层必须是满二叉树。这样我们就只要找到最后一层几个叶子节点就可以判断树中结点的数量。
只有当左右子树高度想同的时候,才能知道该节点之前的树是满的。
当左右子树高度不相等,采用递归的办法,来判断其做左右子树的高度是否相等。
代码:
//根据完全二叉树的特性 除最后一排 前面的树是满二叉树。
public int countNodes(TreeNode root) {
if (root == null)
return 0;
int ans=0;
int left=getLeft(root);
int right=getRight(root);
if(left==right){
return (1<<left)-1;
}else
return 1+countNodes(root.left)+countNodes(root.right);
} private int getLeft(TreeNode root) {
int h=0;
while(root!=null){
h++;
root=root.left;
}
return h;
}
private int getRight(TreeNode root) {
int h=0;
while(root!=null){
h++;
root=root.right;
}
return h;
}
【Leetcode】222. Count Complete Tree Nodes的更多相关文章
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 【leetcode】222. Count Complete Tree Nodes(完全二叉树)
Given the root of a complete binary tree, return the number of the nodes in the tree. According to W ...
- 【刷题-LeetCode】222. Count Complete Tree Nodes
Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Note: Definition ...
- LeetCode OJ 222. Count Complete Tree Nodes
Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium Given a complete binary tree, cou ...
- leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- leetcode面试准备:Count Complete Tree Nodes
1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...
- [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- 【刷题笔记】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 ...
随机推荐
- Android友盟推送
当前版本号:v3.0.5 1.下载SDK解压并导入(import module,compile project(':PushSDK')),里面有demo,用demo的包名去官网添加一个应用,然后替换d ...
- Linux命令——磁盘管理
Linux命令--磁盘管理 命令df 作用:查看已挂载磁盘的总容量.使用容量.剩余容量等 参数:-i,查看inodes的使用状况 参数:-h,使用合适的单位显示(推荐) 命令du 作用:查看某个目录或 ...
- java.lang.NoClassDefFoundError: org.androidpn.client.PersistentConnectionListener
在运行AndroidpnClient项目时出现了java.lang.NoClassDefFoundError: org.androidpn.client.PersistentConnectionLis ...
- POJ 2932 Coneology计算最外层圆个数
平面上有n个两两没有公共点的圆,i号圆的圆心在(xi,yi),半径为ri,编号从1开始.求所有最外层的,即不包含于其他圆内部的圆.输出符合要求的圆的个数和编号.n<=40000. (注意此题无相 ...
- jqgrid 点击列头的超链接或按钮时,不触发列排序事件
接上篇文章:jqgrid 将列头设置为超链接或按钮 如果在列头设置了超链接或按钮,在点击超链接或按钮时会触发列的排序事件. 原由:点击超链接/按钮会触发排序的冒泡事件 解决方法:点击超链接/按钮时,阻 ...
- NanoPC-T2制作刷机包
anoPC-T2制作刷机包 前提:到友善的wiki中,仔细看编译uboot.内核.制作刷机包的教程. 准备工作: 1. 虚拟机Ubuntu安装,并安装n多软件可以支撑编译内核等等. 2. 安装交叉编 ...
- kettle学习笔记(五)——kettle输出步骤
一.概述 数据库表: • 表输出 • 更新,删除,插入/更新 • 批量加载(mysql,oracle) • 数据同步 文件: • SQL 文件输出 • 文本文件输出 • XML 输出 • Excel ...
- 汇编 MOVSX与MOVZX 指令
知识点: MOVSX符号扩展传送 MOVZX零扩展传送 一.MOVSX与MOVZX格式 MOVSX 操作数A ,操作数B MOVZX 操作数A ,操作数B 相同点:操作数B 空间必须小于 操作 ...
- 【Qt】QLabel之动态阴影边框
效果如下: 实现思路参考了下面的文章: Qt 之 QPropertyAnimation 该文章是自定义属性alpha,原理类似,代码如下: //在头文件加入 Q_PROPERTY(int iBorde ...
- CF708D Incorrect Flow
CF708D Incorrect Flow 有源汇上下界最小费用可行流.(= =) 对每条给定的边连边: 首先\(f_i\)是给定的,所以要有一条这个边而且要流满,先\(a_i-b_i\)连一条上下界 ...