LeetCode - Medium - 222,2024年最新html5移动web开发实战

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

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

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024c (备注前端)
img

正文

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++;

}

if (left == null)

return (1 << height) - 1;

return 1 + countNodes(root.left) + countNodes(root.right);

}

//方法五:方法一的对称写法形式

public int countNodes5(TreeNode root) {

if (root == null)

return 0;

int lh = height2(root.left);

int rh = height2(root.right);

if(lh == rh)

return (1 << lh) + countNodes5(root.right); /*1(根节点) + (1<<lh)-1(完全左子树) + # of rightNode */

else

return (1 << rh) + countNodes5(root.left); /1(根节点) + (1<<rh)-1(完全右子树) + # of leftNode/

}

}

Test


import static org.junit.Assert.*;

import org.junit.Test;

import com.lun.util.BinaryTree;

public class CountCompleteTreeNodesTest {

@Test

public void test() {

CountCompleteTreeNodes obj = new CountCompleteTreeNodes();

assertEquals(6, obj.countNodes(BinaryTree.integers2BinaryTree(1, 2, 3, 4, 5, 6)));

assertEquals(1, obj.countNodes(BinaryTree.integers2BinaryTree(1)));

assertEquals(0, obj.countNodes(null));

}

@Test

public void test2() {

CountCompleteTreeNodes obj = new CountCompleteTreeNodes();

assertEquals(6, obj.countNodes2(BinaryTree.integers2BinaryTree(1, 2, 3, 4, 5, 6)));

assertEquals(1, obj.countNodes2(BinaryTree.integers2BinaryTree(1)));

assertEquals(0, obj.countNodes2(null));

}

@Test

public void test3() {

CountCompleteTreeNodes obj = new CountCompleteTreeNodes();

结束

一次完整的面试流程就是这样啦,小编综合了腾讯的面试题做了一份前端面试题PDF文档,里面有面试题的详细解析,分享给小伙伴们,有没有需要的小伙伴们都去领取!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024c (备注前端)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
_convert/45f2869b91b538dd3bb3290ba13bc806.png)

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024c (备注前端)
[外链图片转存中…(img-Q23CRFQj-1713322491315)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值