LeetCode - Medium - 116(1),2024年最新2024历年阿里前端面试真题

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

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

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

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

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

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

正文

You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:

struct Node {

int val;

Node *left;

Node *right;

Node *next;

}

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

Follow up:

  • You may only use constant extra space.

  • Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.

Example 1:

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

Output: [1,#,2,3,#,4,5,6,7,#]

Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with ‘#’ signifying the end of each level.

Constraints:

  • The number of nodes in the given tree is less than 4096.

  • -1000 <= node.val <= 1000

Analysis


方法一:DFS,但递归会用到隐式非常量额外空间,不符题意。

方法二:BFS,但用到非常量额外空间的队列,不符题意。

方法三:子节点们利用父节点的next进行连接。

Submission


import java.util.ArrayList;

import java.util.LinkedList;

import java.util.List;

public class PopulatingNextRightPointersInEachNode {

public static class Node {

public int val;

public Node left;

public Node right;

public Node next;

public Node() {

}

public Node(int _val) {

val = _val;

}

public Node(int _val, Node _left, Node _right, Node _next) {

val = _val;

left = _left;

right = _right;

next = _next;

}

};

// 方法一:DFS

public Node connect(Node root) {

if (root == null)

return root;

connect(root, 0, new ArrayList<>());

return root;

}

private void connect(Node node, int level, List prevs) {

if (node == null)

return;

if (prevs.size() == level) {

prevs.add(node);

} else {

Node prev = prevs.get(level);

prev.next = node;

prevs.set(level, node);

}

connect(node.left, level + 1, prevs);

connect(node.right, level + 1, prevs);

}

// 方法二:BFS

public Node connect2(Node root) {

if (root == null)

return root;

LinkedList queue = new LinkedList<>();

queue.offer(root);

while (!queue.isEmpty()) {

Node prev = null;

for (int size = queue.size(); size > 0; size–) {

Node node = queue.poll();

if (prev != null) {

prev.next = node;

}

prev = node;

if (node.left != null)

queue.offer(node.left);

if (node.right != null)

queue.offer(node.right);

}

}

return root;

}

// 方法三:利用父节点的next

public Node connect3(Node root) {

if (root == null)

return null;

Node pre = root;

Node cur = null;

while (pre.left != null) {

cur = pre;

while (cur != null) {

if (cur.left != null)

最后

总的来说,面试官要是考察思路就会从你实际做过的项目入手,考察你实际编码能力,就会让你在电脑敲代码,看你用什么编辑器、插件、编码习惯等。所以我们在回答面试官问题时,有一个清晰的逻辑思路,清楚知道自己在和面试官说项目说技术时的话就好了

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

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

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

[外链图片转存中…(img-DkXSoG2S-1713322431579)]

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值