LeetCode - Medium - 222

Topic


  • Binary Search

  • Tree

Description


https://leetcode.com/problems/count-complete-tree-nodes/

Given the root of a complete binary tree, return the number of the nodes in the tree.

According to Wikipedia, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between 1 and 2 h 2^h 2h nodes inclusive at the last level h.

Design an algorithm that runs in less than O(n) time complexity.

Example 1:

Input: root = [1,2,3,4,5,6]

Output: 6

Example 2:

Input: root = []

Output: 0

Example 3:

Input: root = [1]

Output: 1

Constraints:

  • The number of nodes in the tree is in the range [ 0 , 5 ∗ 1 0 4 ] [0, 5 * 10^4] [0,5∗104].

  • 0 < = N o d e . v a l < = 5 ∗ 1 0 4 0 <= Node.val <= 5 * 10^4 0<=Node.val<=5∗104

  • The tree is guaranteed to be complete.

Analysis


一个完整的完全树的个数: 1 + 2 + 4 + 8 + . . . + 2 h − 1 1 + 2 + 4 + 8 +… + 2^{h-1} 1+2+4+8+…+2h−1

显然,加法式是以1为首项,公比为2,项个数为h 的等比数列的求和式。

代入求和公式得:

S h = a 1 ( 1 − q h ) 1 − q = 2 h − 1 S_h= \frac {a_1(1-q^h)} {1-q}=2^h-1 Sh​=1−qa1​(1−qh)​=2h−1

换成编程语言S = 1 << h - 1

解题核心思想:根据节点的左右子树的高度比较得出谁是更可能是完整的完全树,谁就能直接用求和公式得出子树的个数,然后在另一个子树递归刚才的过程求个数,最后将递归结果累加得出结果。

方法一:

The height of a tree can be found by just going left. Let a single node tree have height 0. Find the height h of the whole tree. If the whole tree is empty, i.e., has height -1, there are 0 nodes.

Otherwise check whether the height of the right subtree is just one less than that of the whole tree, meaning left and right subtree have the same height.

  • If yes, then the last node on the last tree row is in the right subtree and the left subtree is a full tree of height h-1. So we take the 2^h-1 nodes of the left subtree plus the 1 root node plus recursively the number of nodes in the right subtree.
  • If no, then the last node on the last tree row is in the left subtree and the right subtree is a full tree of height h-2. So we take the 2^(h-1)-1 nodes of the right subtree plus the 1 root node plus recursively the number of nodes in the left subtree.

Since I halve the tree in every recursive step, I have O(log(n)) steps. Finding a height costs O(log(n)). So overall O(log(n)^2).

Link

方法二:个人对方法一作出稍微修改,增加可读性。

方法三:方法一的迭代版本。

方法四:看似暴力计算却暗含优化版。

That would be O(n). But… the actual solution has a gigantic optimization. It first walks all the way left and right to determine the height and whether it’s a full tree, meaning the last row is full. If so, then the answer is just 2^height-1. And since always at least one of the two recursive calls is such a full tree, at least one of the two calls immediately stops. Again we have runtime O(log(n)^2).

Link

方法五:更人钟爱的版本,因为他写法体现对称性。

Submission


import com.lun.util.BinaryTree.TreeNode;

public class CountCompleteTreeNodes {

//方法一:

private int height(TreeNode root) {

return root == null ? -1 : 1 + height(root.left);

}

public int countNodes(TreeNode root) {

int h = height(root);

return h < 0 ? 0 :

height(root.right) == h - 1 ? (1 << h) + countNodes(root.right)
(1 << h - 1) + countNodes(root.left);

}

//方法二:稍微修饰方法一

private int height2(TreeNode root) {

return root == null ? 0 : 1 + height2(root.left);

}

public int countNodes2(TreeNode root) {

int h = height2(root);

return h == 0 ? 0 :

height2(root.right) == h - 1 ? //

(1 << (h - 1)) + countNodes2(root.right) : //

(1 << (h - 2)) + countNodes2(root.left);

}

//方法三:方法一的迭代版

public int countNodes3(TreeNode root) {

int nodes = 0, h = height(root);

while (root != null) {

if (height(root.right) == h - 1) {

nodes += 1 << h;

root = root.right;

} else {

nodes += 1 << h - 1;

root = root.left;

}

h–;

}

return nodes;

}

//方法四:

public int countNodes4(TreeNode root) {

if (root == null)

return 0;

TreeNode left = root, right = root;

int height = 0;

while (right != null) {

left = left.left;

right = right.right;

height++;

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
wxHB6q5r-1715551097419)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 11
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值