既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
[1,2,3,4,5,null,7,8]
1
/ \
2 3
/ \ \
4 5 7
/
8
输出:
[[1],[2,3],[4,5,7],[8]]
---
## 分析
* 面对这道算法题目,二当家的陷入了沉思。
* 如果对二叉树和链表比较熟悉,就会明白其实就是二叉树的层序遍历,每一层组合成一条链表。
---
## 题解
### rust
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell>>,
// pub right: Option<Rc<RefCell>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn list_of_depth(tree: Option<Rc<RefCell>>) -> Vec<Option<Box>> {
let mut ans: Vec<Option<Box>> = Vec::new();
let mut queue = std::collections::VecDeque::new();
queue.push\_back(tree);
while !queue.is\_empty() {
let mut head = Some(Box::new(ListNode::new(0)));
let mut tail = head.as\_mut();
let size = queue.len();
for _ in 0..size {
let node = queue.pop\_front().unwrap().unwrap();
let mut node = node.borrow\_mut();
if node.left.is\_some() {
queue.push\_back(node.left.take());
}
if node.right.is\_some() {
queue.push\_back(node.right.take());
}
tail.as\_mut().unwrap().next = Some(Box::new(ListNode::new(node.val)));
tail = tail.unwrap().next.as\_mut();
}
ans.push(head.as\_mut().unwrap().next.take());
}
ans
}
}
---
### go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func listOfDepth(tree *TreeNode) []*ListNode {
var ans []*ListNode
queue := []*TreeNode{tree}
for len(queue) > 0 {
head := &ListNode{}
tail := head
size := len(queue)
for i := 0; i < size; i++ {
node := queue[i]
if node.Left != nil {
queue = append(queue, node.Left)
}
if node.Right != nil {
queue = append(queue, node.Right)
}
tail.Next = &ListNode{Val: node.Val}
tail = tail.Next
}
ans = append(ans, head.Next)
queue = queue[size:]
}
return ans
}
---
### typescript
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val=undefined ? 0 : val)
* this.left = (left=undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val=undefined ? 0 : val)
* this.next = (next=undefined ? null : next)
* }
* }
*/
function listOfDepth(tree: TreeNode | null): Array<ListNode | null> {
const ans = [];
const queue = [tree];
while (queue.length > 0) {
const head = new ListNode();
let tail = head;
const size = queue.length;
for (let i = 0; i < size; ++i) {
const { val, left, right } = queue.shift();
left && queue.push(left);
right && queue.push(right);
tail.next = new ListNode(val);
tail = tail.next;
}
ans.push(head.next);
}
return ans;
};
---
### python
Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def listOfDepth(self, tree: TreeNode) -> List[ListNode]:
ans = []
q = collections.deque()
q.append(tree)
while len(q) > 0:
head = ListNode()
tail = head
size = len(q)
for _ in range(size):
node = q.popleft()
node.left and q.append(node.left)
node.right and q.append(node.right)
tail.next = ListNode(node.val)
tail = tail.next
ans.append(head.next)
return ans
---
### c
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
int getDepth(struct TreeNode* tree) {
if (!tree) {
return 0;
}
int leftDepth = getDepth(tree->left);
int rightDepth = getDepth(tree->right);
return fmax(leftDepth, rightDepth) + 1;
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
struct ListNode** listOfDepth(struct TreeNode* tree, int* returnSize){
int depth = getDepth(tree);
struct ListNode **ans = malloc(depth * sizeof(struct ListNode *));
*returnSize = 0;
struct TreeNode *queue[(int) pow(2, depth) - 1];
queue[0] = tree;
int start = 0;
int end = 1;
while (start < end) {
struct ListNode head = {};
struct ListNode *tail = &head;
int curEnd = end;
while (start < curEnd) {
struct TreeNode *node = queue[start++];
if (node->left) {
queue[end++] = node->left;
}
if (node->right) {
queue[end++] = node->right;
}
tail->next = malloc(sizeof(struct ListNode));
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
truct ListNode));
[外链图片转存中…(img-w0oOBle9-1715771934803)]
[外链图片转存中…(img-N6bSYr0P-1715771934803)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!