leetcode 102 二叉樹的層次遍歷

2021-09-25 08:39:38 字數 919 閱讀 3977

1.題目:

給定乙個二叉樹,返回其按層次遍歷的節點值。 (即逐層地,從左到右訪問所有節點)。

例如:

給定二叉樹: [3,9,20,null,null,15,7],

3/ \

9 20

/ \

15 7

返回其層次遍歷結果:

[ [3],

[9,20],

[15,7]

]

2.**:

/**

* definition for a binary tree node.

* struct treenode ;

*/ /** * return an array of arrays of size *returnsize.

* the sizes of the arrays are returned as *returncolumnsizes array.

* note: both returned array and *columnsizes array must be malloced, assume caller calls free().

*/#define max 5000

typedef struct treenode* tree;

typedef structqueue;

int** levelorder(struct treenode* root, int* returnsize, int** returncolumnsizes)

}return r;

}

3.知識點:

層次遍歷

LeetCode 102 二叉樹的遍歷

給定乙個二叉樹,返回其按層次遍歷的節點值。即逐層地,從左到右訪問所有節點 例如 給定二叉樹 返回其層次遍歷結果 如下 definition for a binary tree node.public class treenode public class solution list res new ...

LeetCode 102 二叉樹的層次遍歷

題目鏈結 題目描述 給定乙個二叉樹,返回其按層次遍歷的節點值。即逐層地,從左到右訪問所有節點 例如 給定二叉樹 3,9,20,null,null,15,7 3 9 20 15 7 返回其層次遍歷結果 3 9,20 15,7 解決方法 廣度優先 層次 遍歷,使用佇列實現 具體思路 在訪問了乙個節點之後...

LeetCode 102 二叉樹的層次遍歷

給定乙個二叉樹,返回其按層次遍歷的節點值。即逐層地,從左到右訪問所有節點 例如 給定二叉樹 3,9,20,null,null,15,7 3 9 20 15 7返回其層次遍歷結果 3 9,20 15,7 此題需要將各個層的節點分別儲存到不同的陣列中。所以在while迴圈中,加了乙個for迴圈,迴圈次數...