Total Accepted: 22338 Total Submissions: 97234 Difficulty: Medium

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 2hnodes 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 treeDepth(TreeNode* root)
{
int dep=;
while(root){
dep++;
root=root->left;
}
return dep;
}
int countNodes(TreeNode* root) {
if(root==NULL){
return ;
}
int ldep = treeDepth(root->left);
int rdep = treeDepth(root->right);
if(ldep>rdep){
return +countNodes(root->left) + ((<<rdep)-);
}
return +((<<ldep)-) + countNodes(root->right);
}
};

[Tree]Count Complete Tree Nodes的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  7. Count Complete Tree Nodes

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  8. 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 ...

  9. leetcode_222 Count Complete Tree Nodes

    题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree fr ...

随机推荐

  1. WCF入门教程系列二

    一.概述 WCF能够建立一个跨平台的安全.可信赖.事务性的解决方案,是一个WebService,.Net Remoting,Enterprise Service,WSE,MSMQ的并集,有一副很经典的 ...

  2. .C .h 和 .CCP的区别

    1.*.H:C语言规定使用一个变量或调用一个函数前必须声明,为了使用方便,经常把常用函数,例如Windows API的函数,MFC类写入头文件.h,这样每次需要引用时只要使用#include加入就可以 ...

  3. Python学习笔记4(函数与模块)

    1.Python程序的结构 Python的程序由包(package).模块(module)和函数组成. 模块是处理一类问题的集合,由函数和类组成. 包是由一系列模块组成的集合.包是一个完成特定任务的工 ...

  4. (转) Special members

    原地址:http://www.cplusplus.com/doc/tutorial/classes2/   Special members [NOTE: This chapter requires p ...

  5. (a*b)%c 问题

    给你两个数__int64 类型的整数 a ,b,c.问你,(a*b)%c的值是多少??我们知道: (a*b)%c = ((a%c)*(b%c))%c .不过即使这样做,在c很大的情况下,(a%c)*( ...

  6. kinect

    1.学习资料 http://blog.csdn.net/dustpg/article/details/37982311 https://github.com/mdkus/kinect-mssdk-op ...

  7. 有关PHP中点击下载文件的小功能

    最近需要在项目里加一个可以点击导出树状目录的目录结构图, 我在网上查了之后,发现基本千篇一律, 都是使用下面这种header函数, 直接去readfile() 这个文件 header('Content ...

  8. python 在linux下通过top,和dh命令获得cpu,内存,以及硬盘信息

    主要是通过os.popen读取命令输出实现的,os.popen启动新的进程,且将外部命令的输出作为文件类型对象返回.不能获得外部命令的返回值.既然是文件对象就可以直接用for in 来读取,代码如下: ...

  9. Python计算机视觉3:模糊,平滑,去噪

    我是一名初学者,如果你发现文中有错误,请留言告诉我,谢谢 图像的模糊和平滑是同一个层面的意思,平滑的过程就是一个模糊的过程. 而图像的去噪可以通过图像的模糊.平滑来实现(图像去噪还有其他的方法) 那么 ...

  10. HttpWebRequest.Method 属性

    public static void GetHead(string url) { var http = (HttpWebRequest)WebRequest.Create(url); http.Met ...