【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 ...
随机推荐
- CSS 文本
CSS 文本属性可定义文本的外观. 通过文本属性,您可以改变文本的颜色.字符间距,对齐文本,装饰文本,对文本进行缩进,等等. 缩进文本 把 Web 页面上的段落的第一行缩进,这是一种最常用的文本格式化 ...
- 深入C#学习系列一:序列化(Serialize)、反序列化(Deserialize)
序列化概述: 序列化 (Serialization)将对象的状态信息转换为可以存储或传输的形式的过程.在序列化期间,对象将其当前状态写入到临时或持久性存储区.以后,可以通过从存储区中读取或反序列化对象 ...
- P2962 [USACO09NOV]灯Lights
贝希和她的闺密们在她们的牛棚中玩游戏.但是天不从人愿,突然,牛棚的电源跳闸了,所有的灯都被关闭了.贝希是一个很胆小的女生,在伸手不见拇指的无尽的黑暗中,她感到惊恐,痛苦与绝望.她希望您能够帮帮她,把所 ...
- JS关闭窗口而不提示
使用js关闭窗口而不提示代码: window.opener = null; window.open( '', '_self' ); window.close();
- 12、利用docker快速搭建Wordpress网站
一.准备工作 结构图: 用户访问页面,Nginx将请求进行转发,如果请求的是php页面,则通过FastCGI转发给后端php进行处理:如果非php页面,则直接返回静态页面. 关键点: mysql.ph ...
- 设计模式 笔记 代理模式 Proxy
//---------------------------15/04/21---------------------------- //Proxy 代理模式-----对象结构型模式 /* 1:意图: ...
- 设计模式 笔记 单例模式 Singleton
//---------------------------15/04/09---------------------------- //Singleton 单例模式-----对象创建型模式 /* 1: ...
- flask-login 整合 pyjwt + json 简易flask框架
现在很多框架都实现前后端分离,主要为了适应以下几个目的: 1,前后端的分离,可以使前端开发和后端开发更加分工明确,而不是后端还需要在视图模板中加入很多{% XXXX %}标签 2,是为了适应跨域调用或 ...
- gulp.src()内部实现探究
写在前面 本来是想写个如何编写gulp插件的科普文的,突然探究欲又发作了,于是就有了这篇东西...翻了下源码看了下gulp.src()的实现,不禁由衷感慨:肿么这么复杂... 进入正题 首先我们看下g ...
- 开发认为不是bug,你该如何处理?
这是软件测试员面试时经常被问到的问题.看了很多答案,个人觉得作为有工作经验的测试人员回答时不能完全照搬标准答案,技术面试官想听的当然不止如此.毕竟这种情况在实际工作中也常常出现,具体问题要具体分析,你 ...