LeetCode解題報告 102. Binary Tree Level Order Traversal [easy]
題目描述
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]
,
3 / \ 9 20 / \ 15 7
return its level order traversal as:
[ [3], [9,20], [15,7] ]
解題思路
二叉樹的層次遍歷,輸出每一層的節點樹,用巢狀的vector表示。
用一個佇列來記錄當前節點的位置,首先入隊根節點root,跟節點出隊的同時入隊根節點的左右節點;在一次對當前層的遍歷中,將左右節點分別當作根節點,出隊的同時入隊各自對應的左右節點。每次儲存的值為當前根節點的val值,一次遍歷即儲存一行的值。
時間複雜度
bfs,o(n)
程式碼如下:
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> result;
if (root==NULL) {
return result;
}
queue<TreeNode*>currlevel;
currlevel.push(root);
while (!currlevel.empty()) {
vector<int>tmplist;
int l=currlevel.size();
for (int i=0; i<l; i++) {
//cout << currlevel.size() << endl;
TreeNode* currnode=currlevel.front();
currlevel.pop();
tmplist.push_back(currnode->val);
if (currnode->left!=NULL)
currlevel.push(currnode->left);
if (currnode->right!=NULL)
currlevel.push(currnode->right);
}
result.push_back(tmplist);
}
return result;
}
};
這裡說明一下注釋掉的那一行,第一次寫的時候沒有提前宣告l=currlevel.size(),結果導致多出了幾次迴圈,因為在迴圈中currlevel.size()是變化的,而我們應該使用的是迴圈之前的值。相關文章
- Binary Tree Level Order Traversal [LEETCODE]LeetCode
- Leetcode Binary Tree Level Order TraversalLeetCode
- Leetcode Binary Tree Level Order Traversal IILeetCode
- Leetcode-Binary Tree Level Order TraversalLeetCode
- Binary Tree Level Order Traversal leetcode javaLeetCodeJava
- Leetcode Binary Tree Zigzag Level Order TraversalLeetCode
- Leetcode-Binary Tree Level Order Traversal IILeetCode
- Binary Tree Level Order Traversal II leetcode javaLeetCodeJava
- Leetcode-binary Tree Zigzag Level Order TraversalLeetCode
- Binary Tree ZigZag Level Order Traversal leetcode javaLeetCodeJava
- leetcode102:Binary Tree Level Order TraversalLeetCode
- [LeetCode]107. Binary Tree Level Order Traversal IILeetCode
- LeetCode-Binary Tree Vertical Order TraversalLeetCode
- 【LeetCode從零單排】No102 Binary Tree Level Order TraversalLeetCode
- [LintCode] Binary Tree Level Order
- LeetCode 272 Closest Binary Tree Traversal II 解題思路LeetCode
- leetcode - Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Inorder TraversalLeetCode
- Leetcode Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Postorder TraversalLeetCode
- [leetcode]binary-tree-inorder-traversalLeetCode
- Leetcode-Binary Tree Postorder TraversalLeetCode
- Leetcode-Binary Tree Inorder TraversalLeetCode
- Binary Tree Inorder Traversal leetcode javaLeetCodeJava
- Binary Tree Preorder Traversal leetcode javaLeetCodeJava
- Binary Tree Postorder Traversal leetcode javaLeetCodeJava
- LeetCode 94 [Binary Tree Inorder Traversal]LeetCode
- Leetcode-145題:Binary Tree Postorder TraversalLeetCode
- Leetcode 94. Binary Tree Inorder TraversalLeetCode
- Construct Binary Tree from Inorder and Postorder Traversal Traversal leetcode javaStructLeetCodeJava
- LeetCode | 144. Binary Tree Preorder TraversalLeetCode
- LeetCode | 145. Binary Tree Postorder TraversalLeetCode
- Leetcode 144. Binary Tree Preorder TraversalLeetCode
- Leetcode 145. Binary Tree Postorder TraversalLeetCode
- LeetCode145:Binary Tree Postorder TraversalLeetCode
- LeetCode144:Binary Tree Preorder TraversalLeetCode
- leetcode [python] 【144】 Binary Tree Preorder TraversalLeetCodePython
- Leetcode Construct Binary Tree from Inorder and Postorder TraversalLeetCodeStruct