Count Complete Tree Nodes || LeetCode
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
#define MAX 1000
int Path[MAX];
int index1; int power(int x,int n){
int i,re;
if(n==0)return 1;
for(i=0,re=1;i<n;++i){
re=re*x;
}
return re;
} void find_path(struct TreeNode *root){
if( (root->left==NULL&&root->right==NULL) || (root==NULL) )return ;
struct TreeNode *t1,*t2;
t1=root->left,t2=root->right;
while(t1&&t2){
t1 = t1->left;
t2 = t2->left;
}
if(t1){
Path[index1++]=0;
find_path(root->left);
}
else {
Path[index1++]=1;
find_path(root->right);
}
} int countNodes(struct TreeNode* root) {
int nlevel,i,temp,start,end; if(root==NULL)return 0;
if(root->left==NULL && root->right==NULL) return 1;
index1=0;
find_path(root);
nlevel=power(2,index1);
for(i=0,start=1,end=nlevel;i<index1;++i)
{
if(Path[i]==0)
end=(end+start)/2;
else
start=(end+start)/2+1;
if (end==start)break;
}
return nlevel-1+start;
}
Count Complete Tree Nodes || LeetCode的更多相关文章
- Count Complete Tree Nodes ——LeetCode
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 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] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
随机推荐
- Android HttpURLConnection Post 参数 (https)
声明utf-8: public static String CHARSET_UTF8 = HTTP.UTF_8; eg:登陆请求方法,通过接口返回结果: public static void logi ...
- 《DON'T MAKE ME THINK》/《点石成金访客至上的网页设计秘笈》 读书笔记
1.web页面要尽可能简单,让用户不用思考就能知道页面的功能,如果要进行一些崭新的.开拓性的或者非常复杂的页面设计时, 此时要利用页面元素的外观.精心选择的名称.页面布局以及少量仔细斟酌过的文字,使页 ...
- 【BZOJ】2194: 快速傅立叶之二
http://www.lydsy.com/JudgeOnline/problem.php?id=2194 题意:求$c[k]=\sum_{k<=i<n} a[i]b[i-k], n< ...
- 【HDU】1517 A Multiplication Game
http://acm.hdu.edu.cn/showproblem.php?pid=1517 题意:每次乘上2~9..p>=n时赢.. #include <cstdio> #incl ...
- linq中join的用法
join方法 public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>( this IEnu ...
- Maven_如何为开发和生产环境建立不同的配置文件 --我的简洁方案
其实也是最近才看Maven, 以前都是用ant+ivy, 对于轻量级的项目来说足够了, 而且非常灵活. 看了看Maven, 约定.... 现在编程都说约定, 约定是挺好, 问题是超出约定的事情太多了, ...
- PHP slim restfull框架nginx 配置
http://docs.slimframework.com/ 下载地址这个东西很不错,照到官方的例子做 <?php require 'vendor/autoload.php'; $app = n ...
- 使用command对象操作数据库
1.Command对象查询数据库 protected void Button1_Click(object sender, EventArgs e) { //读取web.config节点配置 strin ...
- hbase的查询scan功能注意点(setStartRow, setStopRow)
来自http://hi.baidu.com/7636553/blog/item/982beb17713bc004972b43ee.html hbase的scan查询功能注意项: Scan scan = ...
- 创建 maven 本地仓库
在 pom.xml 添加依赖包的时候,有时候会提示无法从 http://repo1.maven.org/maven2/ 获取的情况,这时可配置个本地仓库: 从网上下载 maven 仓库网站源码包 Ne ...