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 ...
随机推荐
- nginx配置文件中的location中文详解
location 语法:location [=|~|~*|^~] /uri/ { … }默认:否 上下文:server 这个指令随URL不同而接受不同的结构.你可以配置使用常规字符串和正则表达式.如果 ...
- c++ 关于new文件
new文件用来管理c++的动态内存,这个文件声明了几个全局空间的函数(不是std空间的函数,全局空间的函数调用时是用全局作用域解析符),包括operator new 和operator delete的 ...
- leetcode Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- ios 关于[xxx timeIntervalSinceNow]出现EXC_BAD_ACCESS错误的解决办法
[xxx timeIntervalSinceNow]出现EXC_BAD_ACCESS错误的主要原因是之前的[NSDate date]返回一个autoreleased的NSdata,其被释放掉 解决方法 ...
- linux 远程桌面的配置
root根用户登录在/root目录下#rpm -q vnc vnc-server查询已安装vnc的客户端和服务器端vnc-4.1.1-36vnc-server-4.1.1-36 #vi /etc/sy ...
- 51nod算法马拉松13
A 取余最长路 不难发现路径可以拆成三条线段,只要知道两个转折点的位置就能计算出答案. 设sum(i,l,r)表示第i行从l到r元素的和,则答案可以表示为sum(1,1,x)+sum(2,x,y)+s ...
- Maven_非法字符: '\ufeff' 解决方案
Idea在maven打包时报非法字符: '\ufeff' ,但打开报错的类看没有问题,后来发现是隐蔽字符BOM的问题,解决办法是用Notepad++打开这个类,然后改变编码格式为UTF-8 无DOM ...
- 五、点数器《苹果iOS实例编程入门教程》
该app为应用的功能为一个简单的数数程序 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Applic ...
- 李洪强iOS经典面试题133--UNIX常用命令
可能碰到的iOS笔试面试题(3)--UNIX常用命令 做开发说用不到命令行,那肯定是不可能的.所以记住几个常用的命令还是很有用. cd 改变工作目录 pwd 输出当前工作目录的绝对路径在UNIX中要执 ...
- 李洪强漫谈iOS开发[C语言-051]-判断整数位数