题目:
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.

解答:

public class Solution {//学了enum的方法哈哈public enum Direction {LEFT, RIGHT}public int getDepth(TreeNode root, Direction dir) {int depth = 0;//这里是从root开始算起,所以求出来的结果是实际depth + 1while (root != null) {depth++;if (dir == Direction.LEFT) {root = root.left;} else {root = root.right;}}return depth;}public int countNodes(TreeNode root) {if (root == null) return 0;int leftDepth = getDepth(root, Direction.LEFT);int rightDepth = getDepth(root, Direction.RIGHT);if (leftDepth == rightDepth) {//1 << leftDepth是2^(leftDepth)return (1 << leftDepth) - 1;} else {//这一步很巧妙,把根结点加上,然后分治左结点和右结点,如果是叶子结点,在countNodes中就返回1return 1 + countNodes(root.left) + countNodes(root.right);}}
}

222. Count Complete Tree Nodes相关推荐

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

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

  2. Middle-题目95:222. Count Complete Tree Nodes

    题目原文: Given a complete binary tree, count the number of nodes. 题目大意: 给出一个完全二叉树,求节点数. 题目分析: 如果直接递归数节点 ...

  3. leetcode 222 Count Complete Tree Nodes

    今天做了一道leetcode题,刚开始是暴力破解,发现special judge, very large tree 过不去,然后一顿想,一顿查,发现可以运用complete binary tree 的 ...

  4. 222 Count Complete Tree Nodes

    1,这道题如果纯用递归数点而不利用其为一个complete binary tree的话会超时. 2.为了利用这个条件,比较左右两子数的高度:1, 如果相等则左子树为完全二叉树 2, 如果不等, 则右子 ...

  5. LeetCode 222. Count Complete Tree Nodes 题解——Java

    题目链接:https://leetcode.com/problems/count-complete-tree-nodes/#/description 题目要求:计算完全二叉树的节点个数 思路:首先想到 ...

  6. leetcode 222. Count Complete Tree Nodes | 222. 完全二叉树的节点个数(Java)

    题目 https://leetcode.com/problems/count-complete-tree-nodes/ 题解 思路参考左程云<程序员代码面试指南> 顺便贴一下草稿 代码 c ...

  7. Count Complete Tree Nodes

    https://leetcode.com/problems/count-complete-tree-nodes/ 宽度优先搜索方法,超时!! /*** Definition for a binary ...

  8. LeetCode Count Complete Tree Nodes(二分法)

    问题:给出一个完全二叉树,求其结点个数 思路:第一种方式时直接使用递归法,将其左子树个数加上右子树个数再加上根结点 第二种方式二分法,因为完全二叉树除了最后一层外,其它都满足有2^i个结点,而最后一层 ...

  9. LeetCode题解-222-Count Complete Tree Nodes

    没有做出来,所以参考了https://segmentfault.com/a/1190000003818177 迭代法的图解如下:

最新文章

  1. MySQL 学习笔记(3)— 字符串函数、数值函数、日期时间函数、流程函数、聚集函数以及分组数据
  2. CMFCTabCtrl的使用
  3. python的代码有哪些_Python有哪些有趣的代码呢,这些代码让
  4. 分类素材(part5)--大话Python机器学习(中)
  5. 攻破 程序员35岁 “瓶颈” 那都不是事!
  6. Vigenère密码(洛谷P1079题题解,Java语言描述)
  7. 跨境电商ERP管理,让你节约跟多时间!
  8. 基于Task的异步模式的定义
  9. java读取excrl模板填充数据_java中自定义excel模板并且填充内容
  10. Python——免费观看全网视频小程序
  11. SCI/ISTP和EI论文检索号IDS number和收录号查询方法,详细使用教程和指南手册
  12. 对抗雾霾的健康饮食注意
  13. HTML+CSS+JavaScript制作七夕表白网页(含音乐+自定义文字)
  14. Obsidian笔记最新版本的功能Callouts,提升方便性和美观程度
  15. Intel SGX Explained
  16. C#安装包安装提示:已安装了该产品的另一个版本解决方法
  17. MRI影像学习笔记(一)
  18. OpenFlow简单介绍
  19. 电机的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  20. 【四】Java设计模式GOF23之抽象工厂模式

热门文章

  1. Python中使用turtle绘制棋盘详解
  2. jvm执行引擎全解,java解释器即时编译器,全都讲明白
  3. 产品待办列表的几个最佳实践
  4. 一文解读:如何从 0 到 1 打造小程序爆款裂变
  5. CentOS7下解决ifconfig command not found的办法
  6. DC的网络连接端口与防火墙设置[为企业部署Windows Server 2008系列十]
  7. 让XShell保存日志教程
  8. ---------很简单的 一道 堆栈问题-------
  9. 杭州优步uber司机第三组奖励政策
  10. 【JavaScript】父子页面之间跨域通信的方法