当前位置: 首页 > news >正文

LeetCode -- Count Complete Tree Node

题目描述:


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.


统计一个完全树的节点数。


完全树的定义是,高度为h+1的树,前h层的每层节点全满。对于第h+1层,没有叶子的节点均在右侧。意味着,不存在左边有些节点没有叶子而右边有些节点有叶子的情况。


实现思路:
递归求左右高度,如果高度相等,直接返回 Math.Pow(2, h) - 1;如果不等,递归求左子树节点数+ 递归求右子树的节点数 + 1;








实现代码:



/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    
 public int CountNodes(TreeNode root) 
 {
	if(root == null)
	{
		return 0;
	}
		
	var leftH = LeftDepth(root.left);
	var rightH = RightDepth(root.right);


	if(leftH == rightH){
		return (int)Math.Pow(2, leftH + 1) - 1;
	}
	else{
		return CountNodes(root.left) + CountNodes(root.right) + 1;
	}
 }
    public int LeftDepth(TreeNode node){
		var h = 0;
		while(node != null){
			node = node.left;
			h++;
		}
		return h;
    }
	
	public int RightDepth(TreeNode node){
		var h = 0;
		while(node != null){
			node = node.right;
			h++;
		}
		return h;
	}
    
}


相关文章:

  • LeetCode -- Isomorphic Strings
  • LeetCode -- Balanced Binary Tree
  • Linux系统信息查看命令大全
  • LeetCode -- Merge Two sorted lists
  • 汇编语言程序设计的基本方法
  • LeetCode -- Binary Tree Zigzag Level Order Traversal
  • PostgreSQL+PostGIS的使用 1
  • LeetCode -- Compare Version Numbers
  • LeetCode -- Implement Queue using Stacks
  • PostgreSQL+PostGIS的使用 2
  • 有关微软技术方向的最新学习资源【2015-9月】
  • PostgreSQL+PostGIS的使用 3
  • PostgreSQL+PostGIS的使用 4
  • LeetCode -- Longest Substring Without Repeating Characters
  • PostgreSQL+PostGIS的使用 5
  • [case10]使用RSQL实现端到端的动态查询
  • 「前端」从UglifyJSPlugin强制开启css压缩探究webpack插件运行机制
  • 【跃迁之路】【735天】程序员高效学习方法论探索系列(实验阶段492-2019.2.25)...
  • Fundebug计费标准解释:事件数是如何定义的?
  • Golang-长连接-状态推送
  • magento2项目上线注意事项
  • vagrant 添加本地 box 安装 laravel homestead
  • Vue 动态创建 component
  • JavaScript 新语法详解:Class 的私有属性与私有方法 ...
  • $.each()与$(selector).each()
  • (4)事件处理——(7)简单事件(Simple events)
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (Mac上)使用Python进行matplotlib 画图时,中文显示不出来
  • (附源码)spring boot火车票售卖系统 毕业设计 211004
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (官网安装) 基于CentOS 7安装MangoDB和MangoDB Shell
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • (转)C#开发微信门户及应用(1)--开始使用微信接口
  • (转)利用ant在Mac 下自动化打包签名Android程序
  • ***php进行支付宝开发中return_url和notify_url的区别分析
  • .NET Core 2.1路线图
  • .net web项目 调用webService
  • .net 按比例显示图片的缩略图
  • .NET 事件模型教程(二)
  • .net 桌面开发 运行一阵子就自动关闭_聊城旋转门家用价格大约是多少,全自动旋转门,期待合作...
  • .Net6使用WebSocket与前端进行通信
  • .Net语言中的StringBuilder:入门到精通
  • @javax.ws.rs Webservice注解
  • @SuppressWarnings注解
  • [Android]使用Git将项目提交到GitHub
  • [BJDCTF2020]The mystery of ip1
  • [C#][DevPress]事件委托的使用
  • [CISCN2019 华东北赛区]Web2
  • [Markdown] 02 简单应用 第二弹
  • [NOIP2014普及组]子矩阵
  • [one_demo_12]递归打印*\n*.*.\n*..*..\n图形
  • [SSD综述1.8] 固态存储市场发展分析与预测_固态存储技术发展方向(2022to2023)
  • [WeChall] No Escape (Exploit, PHP, MySQL)
  • [WM]写了一周的Native代码的感受
  • [yolov9]使用python部署yolov9的onnx模型