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

Note:

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.

Example:

Input:
1
/ \
2 3
/ \ /
4 5 6 Output: 6
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int countNodes(TreeNode root) {
int left = leftCount(root);
int right = rightCount(root);
if (left != right) {
return 1 + countNodes(root.left) + countNodes(root.right);
} else {
return (1 << left) - 1;
}
} private int leftCount(TreeNode cur) {
int res = 0;
while (cur != null) {
res += 1;
cur = cur.left;
}
return res;
} private int rightCount(TreeNode cur) {
int res = 0;
while (cur != null) {
res += 1;
cur = cur.right;
}
return res;
}
}

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

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

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

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

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

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

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

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

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

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

  6. 222. Count Complete Tree Nodes

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

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

  8. LeetCode OJ 222. Count Complete Tree Nodes

    Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium Given a complete binary tree, cou ...

  9. 【Leetcode】222. Count Complete Tree Nodes

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

随机推荐

  1. 吴裕雄--天生自然 JAVASCRIPT开发学习: DOM

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. .net core编译时设置不自动生成“netcoreapp3.0”目录

    不知道出于什么目的,.netcore项目默认编译时生成的文件要多加一层"netcoreapp3.0"或"netcoreapp2.1",这应该不符合大多数开发者的 ...

  3. CentOS8上用Docker部署开源项目Tcloud

    一.安装Docker 1.我是虚拟机装的Centos7,linux 3.10 内核,docker官方说至少3.8以上,建议3.10以上(ubuntu下要linux内核3.8以上) root账户登录,查 ...

  4. 12 Spring Data JPA:springDataJpa的运行原理以及基本操作(上)

    spring data jpaday1:orm思想和hibernate以及jpa的概述和jpa的基本操作 day2:springdatajpa的运行原理 day2:springdatajpa的基本操作 ...

  5. cin cout

    编写一个程序,要求用户输入一串整数和任意数目的空格,这些整数必须位于同一行中,但允许出现在该行的任何位置.当用户按下enter键,数据输入停止.程序自动对所有的整数进行求和并打印出结果. 需要解决两个 ...

  6. pandas在指定列插入数据

    import pandas as pd import numpy as np df = pd.DataFrame(np.arange(15).reshape(5, 3), columns=['a', ...

  7. 数据分析-Matplotlib:绘图和可视化

    学习路线 简介 简单绘制线形图 plot函数 支持图类型 保存图表 1.简介 Matplotlib是一个强大的Python绘图和数据可视化的工具包.数据可视化也是我们数据分析的最重要的工作之一,可以帮 ...

  8. #JS# 如何判断一个字符串是否为日期格式

    var data = “2018-12-09”; //返回为false则是日期格式;isNaN(data)排除data为纯数字的情况(此处不考虑只有年份的日期,如‘2018’) if(isNaN(da ...

  9. KMP算法复杂度证明

    引言 KMP算法应该是看了一次又一次,比赛的时候字符串不是我负责,所以学到的东西又还给网上的博客了-- 退役后再翻开看,看到模板,心想这不是\(O(n^2)\)的复杂度吗? 有两个循环也不能看做是\( ...

  10. Golang 热编译rizla 插件

    今天在写gin接口的时候,每次添加或修改个接口都需要重启项目才能测试,感觉很麻烦. 因为beego有bee工具,bee run启动项目fsnotify会监控文件变动,经查发现在iris项目中有个插件支 ...