【leetcode】222. Count Complete Tree Nodes(完全二叉树)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int countNodes(TreeNode* root) {
//为啥要设计一个时间复杂度小于n的算法
//完全二叉树的特性 o(n)的比较好写
// 先写o(n)复杂的的
int res=0;
if(root==nullptr) return res;
stack<TreeNode* > dp;
dp.push(root);
while(!dp.empty()){
TreeNode* node=dp.top();
res++;
dp.pop();
if(node->left!=nullptr){
dp.push(node->left);
}
if(node->right!=nullptr){
dp.push(node->right);
}
}
return res;
}
};
/**
* 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:
bool isOK(TreeNode *root, int h, int v) {
TreeNode *p = root;
for (int i = h - 2; i >= 0; --i) {
if (v & (1 << i)) p = p->right;
else p = p->left;
}
return p != NULL;
} int countNodes(TreeNode* root) {
if (root == NULL) return 0;
TreeNode *p = root;
int h = 0;
while (p != NULL) {
p = p->left;
++h;
}
int l = 0, r = (1 << (h - 1)) - 1, m;
while (l <= r) {
m = l + ((r - l) >> 1);
if (isOK(root, h, m)) l = m + 1;
else r = m - 1;
}
return (1 << (h - 1)) + r;
}
};
【leetcode】222. 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 ...
- (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 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 ...
随机推荐
- iptables 原理及应用
转自:iptables 原理及应用 iptables是一个Linux下优秀的nat+防火墙工具,我使用该工具以较低配置的传统pc配置了一个灵活强劲的防火墙+nat系统,小有心得,看了网上也有很多这方面 ...
- centos7 永久修改hostname
1.修改 /etc/sysconfig/network ,重启后还是原来的主机名 2.百度有说明是/etc/rc.d/rc.sysinit下的脚本还原了还来主机名,问题是的我主机上没有这个脚本 3.执 ...
- hbuilder中webview调试console.log无法输出日志的问题
遇到这个问题的亲,肯定是用的模拟器来测试的,其实你只要换成真机测试就能打印了,前提是安卓系统. 有问题欢迎留言,如果你觉得这个文章对你有帮助,就请点个赞吧!
- dart系列之:元世界pubspec.yaml文件详解
目录 简介 pubspec.yaml支持的字段 一个例子 字段详情 总结 简介 pubspec.yaml是所有dart项目的灵魂,它包含了所有dart项目的依赖信息和其他元信息,所以pubspec.y ...
- [loj3031]聚会
对于一棵树(初始仅包含节点0),不断加入一个不在树中的节点$u$(不需要随机),并维护这棵树 具体的,对这棵树点分治,假设当前重心$v$有$d$个子树,假设其中第$i$个子树根为$r_{i}$,子树大 ...
- [luogu5654]基础函数练习题
答案即区间$[l,r]$的笛卡尔树上,左右子树有一个为空的点到根路径和(定义此为的该点答案)的max, 对求区间笛卡尔树复杂度为$o(n)$,无法通过,因此在全局笛卡尔树中考虑此问题 设$k$为$l$ ...
- [bzoj2756]奇怪的游戏
对棋盘黑白染色后,若n和m都是奇数(即白色和黑色点数不同),可以直接算得答案(根据白-黑不变):若n和m不都是奇数,二分答案(二分的上限要大一点,开$2^50$),最后都要用用网络流来判定.考虑判定, ...
- [noi1774]array
容易想到树套树,但数据范围太大,会超时 考虑平衡树的作用,就是将这个区间内的所有数排序,所以可以离线+归并来处理,预处理复杂度$o(n\log n)$,然后考虑维护:1.删除:2.询问 删除操作维护可 ...
- 一文带你吃透CLR垃圾回收机制
前言 今天我们来共同学习一下CLR的垃圾回收机制,这对我们写出健壮性的代码很有帮助,也许有人会认为多此一举,认为垃圾回收交给CLR就行,我不用关心这个,诚然,大多数情况下是这样的,但是,我们今天讨论的 ...
- Go语言程序结构之变量
初识Go语言之变量 var声明创建一个具体类型的变量,然后给它附加一个名字,设置他的初始值,这种声明都是一个通用的形式: var name type = expression 在实际的开发中,为了方便 ...