LeetCode Count Complete Tree Nodes
原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/
题目:
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.
题解:
Complete Tree的定义是左右深度差不大于1.
递归调用函数,终止条件两个,一个是root == null, return 0. 另一个是左右高度相同说明是满树, return 2^height-1.
若是左右高度不同,递归调用求 左子树包含Node数 + 右子树包含Node数 + 1(自身).
Note:1. 用Math.pow(), 注意arguments 和 return type 都是double, 此处会TLE.
2. 用<<来完成幂运算,但注意<<的precedence比+,-还要低,需要加括号.
Time Complexity: O(h^2). worst case对于每一层都要求一遍当前点到leaf得深度.
假设只有最底层多了一个TreeNode 那么计算height就需要每层计算一遍h + (h-1) + (h-2) + ... + 1 = O(h^2).
Space: O(h), stack space.
AC Java:
/**
* 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 p = root;
TreeNode q = root;
int leftDepth = 0;
int rightDepth = 0;
while(p!=null){
leftDepth++;
p = p.left;
}
while(q!=null){
rightDepth++;
q = q.right;
} if(leftDepth == rightDepth){
return (1<<leftDepth) - 1;
}else{
return countNodes(root.left) + 1 + countNodes(root.right);
}
}
}
LeetCode Count Complete Tree Nodes的更多相关文章
- [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
Description: Given a complete binary tree, count the number of nodes. In a complete binary tree ever ...
- 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 ...
随机推荐
- 如何获取checkboxlist的多个选中项
string[] array = dt.Rows[0]["s_type"].ToString().Split('|'); foreach (ListI ...
- 转 c# 日期函数[string.Format----GetDateTimeFormats]格式 .
DateTime dt = DateTime.Now;Label1.Text = dt.ToString();//2005-11-5 13:21:25Label2.Text = dt.ToFileTi ...
- loadView、viewDidLoad及viewDidUnload的关系
标题中所说的3个方法,都是UIViewController的方法,跟UIViewController的view属性的生命周期息息相关.接下来我会一一阐述它们的作用以及它们之间的联系. loadVi ...
- 李洪强-C语言2-字符串
C语言字符串 一.字符串基础 注意:字符串一定以\0结尾. Printf(“yang\n”); 其中yang为字符串常量,“yang”=‘y’+‘a’+‘n’+‘g’+‘\0’.字符串由很多的字符 ...
- linux中执行命令权限不够怎样处理
在linux中执行命令权限不够就要增加权限,先看遇到的情况 查看权限情况 那就赋予权限 执行命令
- Phaser.Game这个函数都有哪些参数
Phaser是一个简单易用且功能强大的html5游戏框架,利用它可以很轻松的开发出一个html5游戏.在这篇文章中我就教大家如何用Phaser来制作一个前段时间很火爆的游戏:Flappy Bird,希 ...
- PowerShell - Read an Excel file using COM Interface
http://www.lazywinadmin.com/2014/03/powershell-read-excel-file-using-com.html
- Editplus从下载到使用
☆ 准备工作 1,保证浏览器正常上网 2,能下载软件或已经下载到Editplus这个工具. ☆ 下载editplus 在浏览器输入http://www.editplus.com,然后回车.进入edit ...
- PHP 开发 APP 接口 学习笔记与总结 - 静态缓存
存储静态缓存即把缓存写入文件. file.php <?php class Cache{ //静态缓存文件后缀名 const EXT = 'txt'; //定义缓存文件存放路径 private $ ...
- Ubuntu下设置中文字符集支持(解决中文乱码问题)
一. Ubuntu默认的中文字符编码 Ubuntu默认的中文字符编码为zh_CN.UTF-8,这个可以在/etc/environment中看到: sudo gedit /etc/environment ...