【算法leetcode】面试题 04,大数据开发程序员怎么优雅迈过30K+这道坎

24 阅读3分钟

img img

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

需要这份系统化资料的朋友,可以戳这里获取

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

    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)); tail->next->val = node->val; tail->next->next = NULL; tail = tail->next; } ans[(*returnSize)++] = head.next; }

return ans;

}




---



### c++



/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: vector<ListNode*> listOfDepth(TreeNode* tree) { vector<ListNode *> ans;

    queue<TreeNode \*> q;
    q.push(tree);

    while (q.size() > 0) {
        ListNode head = ListNode(0);
        ListNode \*tail = &head;
        int size = q.size();
        for (int i = 0; i < size; ++i) {
            TreeNode \*node = q.front();
            q.pop();
            if (node->left != NULL) {
                q.push(node->left);
            }
            if (node->right != NULL) {
                q.push(node->right);
            }
            tail->next = new ListNode(node->val);
            tail = tail->next;
        }
        ans.emplace\_back(head.next);
    }

    return ans;
}

};




---



### java



/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode[] listOfDepth(TreeNode tree) { List list = new ArrayList<>();

    Queue<TreeNode> queue = new LinkedList<>();
    queue.add(tree);

    while (!queue.isEmpty()) {
        ListNode head = new ListNode();
        ListNode tail = head;
        int      size = queue.size();
        for (int i = 0; i < size; ++i) {
            TreeNode node = queue.poll();
            if (node.left != null) {
                queue.add(node.left);
            }
            if (node.right != null) {
                queue.add(node.right);
            }
            tail.next = new ListNode(node.val);
            tail = tail.next;
        }
        list.add(head.next);
    }

    ListNode[] ans = new ListNode[list.size()];
    list.toArray(ans);

    return ans;
}

}




---



## [原题传送门:https://leetcode.cn/problems/list-of-depth-lcci/submissions/](https://gitee.com/vip204888)




---




> 
> 非常感谢你阅读本文~  
>  欢迎【点赞】【收藏】【评论】~  
>  放弃不难,但坚持一定很酷~  
>  希望我们大家都能每天进步一点点~  
>  本文由 [二当家的白帽子:https://le-yi.blog.csdn.net/](https://gitee.com/vip204888) 博客原创~
> 
> 


![img](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/d861607a0acd4cd68933efdb4d4d7a46~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3MzM5MTQ5MjgwNjA=:q75.awebp?rk3s=f64ab15b&x-expires=1772524857&x-signature=t7LAYN%2FifaO9boiC4SQAW7iU9JA%3D)
![img](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/a0214dc8350e4b9ca29049b889998561~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3MzM5MTQ5MjgwNjA=:q75.awebp?rk3s=f64ab15b&x-expires=1772524857&x-signature=n26GnL539YyMcR3urDlAU3bxY%2Bo%3D)

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

**[需要这份系统化资料的朋友,可以戳这里获取](https://gitee.com/vip204888)**


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