题目:

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. 会说话的ABAP report

    report z. INCLUDE ole2incl. DATA: ole   TYPE ole2_object,       voice TYPE ole2_object,       text   ...

  2. 寄生组合式继承 js

    寄生组合式继承是集寄生式继承和组合继承的优点于一身,是基于类型继承最有效的方式 function object(o){ function F(){}; F.prototype = o; return ...

  3. Android(java)学习笔记61:Android中的 Application类用法

    1. 简介 如果想在整个应用中使用全局变量,在java中一般是使用静态变量,public类型:而在android中如果使用这样的全局变量就不符合Android的框架架构,但是可以使用一种更优雅的方式就 ...

  4. E. New Reform_贪心,深搜,广搜。

    E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. Linux空间PHP开发环境小白教程(LAMP)

    租了一个云服务器, 但是只有linux系统,没有php开发环境, 只好自己摸索着一步一步安装啦. 本教程来自自学IT创E老师的Linux教程,想详细了解的可以去论坛找. 一.使用PUTTY登录服务器 ...

  6. N76E003---输入捕获

    输入捕获 根据芯片手册,定时器2可以作为输入捕获使用,设置非常简单,官方也提供了宏给我们使用 void Time2_cap_init(void) { /******* 输入捕获CF设置 ******* ...

  7. P1266 速度限制

    P1266 速度限制 第一次接触这种分层spfa 类似于dp 个人理解 #include<cstdio> #include<iostream> #include<algo ...

  8. Git永久删除commit--[非教程]

    假设当前分支为master,当前的commit情况如下,现在需要删除commit_id_2和commit_id_4: commit_id_1 commit_id_2 commit_id_3 commi ...

  9. 旧文备份:rtlinux安装手册

    前段时间接触了几天RTLinux,折腾了好几天才总算把它安装上,得益于Prof. Chang-Gun Lee的安装建议,觉得该文档可能会对准备尝试安装RTLinux的朋友们有帮助,本人英语很烂,也比较 ...

  10. EL 和 JSTL 的使用

    EL Express Language 表达式语言 就是把<% 这里可以写java语言 %> 这种jsp的写法简化变为${ }的方式 例如 action="${pageConte ...