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 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
随机推荐
- Windows系统Python 安装第三方模块时,提示pip版本有问题
如果按照提示输入python -m pip install --upgrade pip 还不行, 那么执行easy_install --upgrade pip 即可 参考:https://stacko ...
- Linux中vim命令出现E325错误解决方法
出现该问题的原因是: vim在编辑文件的时候会创建一个swp file来保证文件的安全性,如果没有正常退出vim的,下次打开这个文件就会报E325的错误,提示swp文件已经存在. 解决方法也简单:把这 ...
- 023-Spring Boot 服务的注册和发现
一.概述 服务调用 1.1.nginx方式 1.2.注册中心 二.注册中心[zookeeper] 2.1.安装zookeeper3.4.11 2.2.服务提供方,需要在服务启动时吗.,把服务的信息(I ...
- 玩转type类型(牛逼克拉斯 )
一.前言 一说起type()方法肯定有很多人都知道这不就是查看某个对象类型的嘛,其实不然,他还有更牛逼的应用------创建类 >>> type(1) <class 'int' ...
- set去重,session,cookie c#与python 对比
端口,发送请求进行监听,然后处理 session 是存储在服务器端的数据,靠sessionId来验证获取信息,没有大小和类型限制, cookie 是存储在客户端的数据,可以长期使用,有面临被获取的 ...
- [笔记]一道C语言面试题:IPv4字符串转为UInt整数
题目:输入一个IPv4字符串,如“1.2.3.4”,输出对应的无符号整数,如本例输出为 0x01020304. 来源:某500强企业面试题目 思路:从尾部扫描到头部,一旦发现无法转换,立即返回,减少无 ...
- asp.net 文件上传
前台js <script type="text/javascript"> window.onload = function () { document.getEleme ...
- 【HackerRank】Utopian tree
The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occu ...
- scp 和 pscp
今天在做项目遇到了两个问题,做一些总结. 1.在项目中,防火墙的的相关配置是关于cisco企业级防火墙的配置,并不是window防火墙. 2. 在Linux上, scp linux文件 ...
- 20145230《Java程序设计》第5周学习总结
20145230 <Java程序设计>第5周学习总结 教材学习内容 本周主要学习的内容是关于异常处理的,感觉这部分内容对我们这种初学者 来说非常重要.举个例子,倘若你在编写一个Java程序 ...