题目:

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.

代码:

/**
* 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 ;
int hl = ;
TreeNode* l = root->left;
int hr = ;
TreeNode* r = root->right;
while ( l ){
++hl;
l = l->left;
}
while ( r ){
++hr;
r = r->right;
}
if ( hr==hl ) return pow(, hr)-;
return Solution::countNodes(root->left) + Solution::countNodes(root->right) + ;
}
};

tips:

学习大神的递归代码:http://bookshadow.com/weblog/2015/06/06/leetcode-count-complete-tree-nodes/

(1)如果left height=right height则是full tree可以直接返回节点数

(2)如果left height!=right height则不是full tree,分别递归求解left和right的节点数

【Count Complete Tree Nodes】cpp的更多相关文章

  1. 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

  2. 【刷题-LeetCode】222. Count Complete Tree Nodes

    Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Note: Definition ...

  3. leetcode面试准备:Count Complete Tree Nodes

    1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...

  4. leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes

    完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...

  5. 完全二叉树的节点个数 Count Complete Tree Nodes

    2018-09-25 16:36:25 问题描述: 问题求解: 单纯遍历了一遍,emmm,果然TLE. 解题思路就是比较左边树高度和右边树高度,如果相等,那么就是一个满二叉树,返回1 << ...

  6. LeetCode Count Complete Tree Nodes

    原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...

  7. 【Leetcode】222. Count Complete Tree Nodes

    Question: Given a complete binary tree, count the number of nodes. Definition of a complete binary t ...

  8. 【刷题笔记】LeetCode 222. Count Complete Tree Nodes

    题意 给一棵 complete binary tree,数数看一共有多少个结点.做题链接 直观做法:递归 var countNodes = function(root) { if(root===nul ...

  9. 【leetcode】222. Count Complete Tree Nodes(完全二叉树)

    Given the root of a complete binary tree, return the number of the nodes in the tree. According to W ...

随机推荐

  1. 我的Java修养

    无论如何我都会以这种方式严于律己,如有错误接受修正. 1.戒掉对友情和爱情的幻想 2.针对人的行为进行分析,而不是其语言 3.解决一个问题,这个问题会成为解决后续问题的基础和前提 4.不要炫耀,自负, ...

  2. MMU CPU及思想

    要素: 1)CPU访问寻址地址空间: 2)内存不足以容纳所有进程数据: 3)MMU将进程数据分割,保留当前使用数据. http://baike.baidu.com/link?url=KHyp37Ysi ...

  3. nbu备份虚拟机报错156状态4207

    VMware Backup getting snapshot error encountered (156)and status: 4207: Could not fetch snapshot met ...

  4. sz 命令

    sz命令 下载文件命令 sz  文件名

  5. Json数据常用操作

    JSON字符串: var str1 = '{ "name": "cs", "sex": "man" }'; JSON对象 ...

  6. avalon.js的循环操作在表格中的应用

    avalon.js的循环操作在表格中的应用 一个JAVA开发,因为做的门户系统中,数据的展示加载的速度很影响使用效果,想到的是尽量少的请求后台,然后接触到了avalon,看介绍这是一个很轻很轻的MVV ...

  7. 时间复杂度 log n

    时间复杂度 O(log n) 意味着什么? 预先知道算法的复杂度是一回事,了解其后的原理是另一件事情. 不管你是计算机科班出身还是想有效解决最优化问题,如果想要用自己的知识解决实际问题,你都必须理解时 ...

  8. Python 遗传算法实现字符串

    Python 遗传算法实现字符串 流程 1. 初始化 2. 适应度函数 3. 选择 4. 交叉 5. 变异 适应度函数计算方法 计算个体间的差:分别计算每个元素与目标元素的差取平方和 种群:计算总体均 ...

  9. LNMP+HAProxy+Keepalived负载均衡 - LNMP基础环境准备

    环境版本说明: 服务器系统:CentOS 7.5: ``` cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) # 输出结果 `` ...

  10. PHP自动生成分页链接

    page.class.php <?php class Page { // 分页栏每页显示的页数 public $rollPage = 5; // 页数跳转时要带的参数 public $param ...