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 ...
随机推荐
- kubernetes健康检查
有时候容器在running的状态,但是里面的服务挂了,这个就难办了,所以k8s提供了一种检查服务是否健康的方法 Liveness Probe的种类: ● ExecAction:在container中执 ...
- /dev/null脚本中作用
/dev/null设备文件只有一个作用,往它里面写任何数据都直接丢弃. 因此保证了该命令执行时屏幕上没有任何输出. 在shell中常见. command > /dev/null 2> ...
- C#操作摄像头 实现拍照功能
从正式工作以来一直做的都是基于B/S的Web开发,已经很长时间不研究C/S的东西了,但是受朋友的委托,帮他做一下拍照的这么个小功能.其实类似的代码网上有很多,但是真的能够拿来运行的估计也没几个.本来是 ...
- Zookeeper使用场景
分布式系统的运行是很复杂的,因为涉及到了网络通信还有节点失效等不可控的情况.下面介绍在最传统的master-workers模型,主要可以会遇到什么问题,传统方法是怎么解决以及怎么用zookeeper解 ...
- Aria2 懒人安装教程
https://aria2.github.io/ uI版:https://github.com/ziahamza/webui-aria2 web的 可以在线使用的 https://ziahamza.g ...
- laravel 5.1 性能优化对比 - 框架提供的方法
写了一个项目发现性能不如人意. 于是便测试下, 看下性能瓶颈在什么地方. 使用 ab -n 20 http://www.lartest.com/ 软件环境: OS : windows 8.1 CPU: ...
- 用route命令解决多出口的问题
网络已经走进了我们的生活.工作.学习之中,大多数单位.公司都已经连接到了Internet.但是,因为各种原因,有这样一个问题存在.就是:这些单位即有到公网(Internet)的出口连接,也有到专网(单 ...
- 关于Cocos2d-x中字体的使用
1.如果使用的是系统自带的 static Label* createWithSystemFont ( const std::string & text, const std::s ...
- 解决ERROR_INVALID_USER_BUFFER
我用CSocket编写了一个HTTP下载程序,接收数据大概如下: //...... //use CSocket class //send request... //...... ); ) { Zero ...
- IOS 命令行编译
转自:简书 IOS 命令行编译 发表于 IOS2013-08-17 07:07 字数: 583 阅读量: 61 This document will note about the ios comm ...