Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree[3,9,20,null,null,15,7] ,
Given binary tree
3 / \ 9 20 / \ 15 7
return its level order traversal as:
[ [3], [9,20], [15,7] ]<Solution>
這題是要把在同高度的 node 的值 group 在一起
想法如下
- 一層一層處理,把同一層的 node,按照從左至右的順序放到 queue
- 只要 queue 不為空,就代表還沒歷遍完
- 當下 queue 的 size,就代表有多少個 node 在同一層 level,逐一取出來,並將值放到答案
c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: | |
vector<vector<int>> levelOrder(TreeNode* root) { | |
vector<vector<int>> ans; | |
queue<TreeNode *> nodeQue; | |
if(root) { | |
nodeQue.push(root); | |
} | |
int size; | |
vector<int> out; | |
while(!nodeQue.empty()) { | |
out.clear(); | |
size = nodeQue.size(); | |
//> get all nodes in current level | |
for(int i = 0; i < size; i++) { | |
TreeNode *tmpNode = nodeQue.front(); | |
nodeQue.pop(); | |
out.push_back(tmpNode->val); | |
//> if current node has left node | |
if(tmpNode->left) { | |
nodeQue.push(tmpNode->left); | |
} | |
//> if current node has right node | |
if(tmpNode->right) { | |
nodeQue.push(tmpNode->right); | |
} | |
} | |
//> put all values of nodes of same level into ans | |
ans.push_back(out); | |
} | |
return ans; | |
} | |
}; |
kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Example: | |
* var ti = TreeNode(5) | |
* var v = ti.`val` | |
* Definition for a binary tree node. | |
* class TreeNode(var `val`: Int) { | |
* var left: TreeNode? = null | |
* var right: TreeNode? = null | |
* } | |
*/ | |
class Solution { | |
fun levelOrder(root: TreeNode?): List<List<Int>> { | |
val ans = mutableListOf<List<Int>>() | |
val queue = mutableListOf<TreeNode>() | |
root?.run { queue.add(this) } | |
while(queue.isNotEmpty()) { | |
val size = queue.size | |
val output = mutableListOf<Int>() | |
for(i in 0 until size) { | |
val tmp = queue.first() | |
output.add(tmp.`val`) | |
queue.removeAt(0) | |
tmp.left?.let { queue.add(it) } | |
tmp.right?.let { queue.add(it) } | |
} | |
ans.add(output) | |
} | |
return ans | |
} | |
} |
想法也是一樣,根據 level 去將每個 node 的值填到對應的 vector 上
code 如下
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
private: | |
vector<vector<int>> ans; | |
public: | |
vector<vector<int>> levelOrder(TreeNode* root) { | |
recursive(root, 0); | |
return ans; | |
} | |
void recursive(TreeNode *node, const int &level) { | |
if(!node) { | |
return; | |
} | |
//> current node is in new level | |
if(ans.size() == level) { | |
ans.push_back({}); | |
} | |
ans[level].push_back(node->val); | |
recursive(node->left, level+1); | |
recursive(node->right, level+1); | |
} | |
}; |
沒有留言:
張貼留言