LeetCode OJ: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.
数二叉树的节点数首先肯定是可以用暴力解法去解问题的,但是这样总是会timeout:
class Solution {
public:
int countNodes(TreeNode* root) {
if(!root) return ;
total++;
countNodes(root->left);
countNodes(root->right);
return total;
}
private:
int total;
};
其他的办法就是对完全二叉树而言,其一直向左走和一直向右边、走只可能是相同的或者相差1,如果相同那么起节点个数就是2^h - 1否曾再递归的加上左右节点的和就可以了。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int countNodes(TreeNode* root) {
if(!root) return ;
TreeNode * leftNode = root;
TreeNode * rightNode = root;
int left = ;
int right = ;
while(leftNode->left){
left++;
leftNode=leftNode->left;
}
while(rightNode->right){
right++;
rightNode = rightNode->right;
}
if(left == right) return ( << (left + )) - ;
else return + countNodes(root->left) + countNodes(root->right);
}
};
写的比较乱哈 ,凑合着看看把。
下面是java代码,首先肯定是递归形式的,不出所料,同样会TLE:
/**
* 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;
else
return 1 + countNodes(root.left) + countNodes(root.right);
}
}
考虑到完全二叉树的性质,下面这个写法就不会TLE了:
public class Solution {
public int countNodes(TreeNode root) {
if(root == null)
return 0;
int right = 1, left = 1;
TreeNode leftNode = root;
TreeNode rightNode = root;
while(leftNode.left != null){
leftNode = leftNode.left;
left++;
}
while(rightNode.right != null){
rightNode = rightNode.right;
right++;
}
if(left == right)
return (1 << left) - 1;
else
return 1 + countNodes(root.left) + countNodes(root.right); //注意这里不要忘记计算本身的这个节点
}
}
LeetCode OJ:Count Complete Tree Nodes(完全二叉树的节点数目)的更多相关文章
- [leetcode]222. Count Complete Tree Nodes完全二叉树的节点数
/* 满二叉树的特点是2^n-1,对于完全二叉树,一个node如果左右子树深度相同,那么 是一个满二叉树.如果不是,那就把node算上,继续往下看,下边的可能是满二叉树 由于完全二叉树中有一些子满二叉 ...
- [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 ...
- leetcode之Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- (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 ...
- 222 Count Complete Tree Nodes 完全二叉树的节点个数
给出一个完全二叉树,求出该树的节点个数.完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底层为第 h ...
- leetcode 222.Count Complete Tree Nodes
完全二叉树是从左边开始一点点填充节点的,因此需要计算所有的节点的个数. 则分别从左边和右边来进行传递的,当左右是完全二叉树的时候,其节点个数就是pow(2,h)-1. /** * Definition ...
- 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 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
随机推荐
- 顽石系列:CSS实现垂直居中的五种方法
顽石系列:CSS实现垂直居中的五种方法 在开发过程中,我们可能沿用或者试探性地去使用某种方法实现元素居中,但是对各种居中方法的以及使用场景很不清晰.参考的内容链接大概如下: 行内元素:https:// ...
- HackerRank - fibonacci-modified 【大数】
思路 用PYTHON 或 JAVA 干掉 AC代码 a, b, n = map(int, input().split()) for i in range (2, n, 1) : temp = b b ...
- PAT 天梯赛 L1-038. 新世界 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-038 AC代码 #include <iostream> #include <cstdio&g ...
- Linux下32位与64位数据类型大小
Redhat Enterprise Linux 32 Redhat Enterprise Linux 64
- Array排序方法sort()中的大坑
sort() 方法用于对数组的元素进行排序. 但是排序结果就有点坑了,都不按常规出牌的: // 看上去正常的结果: ['Google', 'Apple', 'Microsoft'].sort(); / ...
- 【leetcode刷题笔记】Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- python中类(class)和实例(instance)
面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可 ...
- Nginx配置指令的执行顺序
rewrite阶段 rewrite阶段是一个比较早的请求处理阶段,这个阶段的配置指令一般用来对当前请求进行各种修改(比如对URI和URL参数进行改写),或者创建并初始化一系列后续处理阶段可能需要的Ng ...
- 20145240 《Java程序设计》第四周学习总结
20145240 <Java程序设计>第四周学习总结 教材学习内容总结 6.1继承 6.1.1 继承共同行为 定义:继承基本上就是避免多个类间重复定义共同行为. 优点:1.提高了代码的复用 ...
- 线性代数:A转置乘以A可逆
如果A的列向量线性无关,则 T(A)*A得到一个可逆的方阵. 假设A是一个kxn的矩阵,那么T(A)*A是一个nxn的方阵:要证明这个方阵可逆,只要证明N(T(A)*A) = 零空间即可. 假设列向量 ...