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
- Binary-tree-level-order-traversal
- 102-Binary Tree Level Order Traversal
- 107-Binary Tree Level Order Traversal II
- 103. Binary Tree Zigzag Level Order Traversal
- [LintCode] Binary Tree Level Order
- LeetCode 272 Closest Binary Tree Traversal II 解題思路LeetCode
- [leetcode]binary-tree-inorder-traversalLeetCode
- LeetCode | 144. Binary Tree Preorder TraversalLeetCode
- LeetCode | 145. Binary Tree Postorder TraversalLeetCode
- Leetcode 94. Binary Tree Inorder TraversalLeetCode
- Leetcode 144. Binary Tree Preorder TraversalLeetCode
- Leetcode 145. Binary Tree Postorder TraversalLeetCode
- Leetcode Binary Tree PathsLeetCode
- [LeetCode] 226. Invert Binary TreeLeetCode
- [LeetCode] 543. Diameter of Binary TreeLeetCode
- LeetCode 543. Diameter of Binary TreeLeetCode
- LeetCode545.Boundary-of-Binary-TreeLeetCode
- Leetcode 226. Invert Binary TreeLeetCode
- [leetcode]maximum-depth-of-binary-treeLeetCode
- LeetCode之Univalued Binary Tree(Kotlin)LeetCodeKotlin
- LeetCode之Binary Tree Pruning(Kotlin)LeetCodeKotlin
- Leetcode - Tree - Easy(100-110)LeetCode
- LeetCode之Increasing Order Search Tree(Kotlin)LeetCodeKotlin
- LeetCode のminimum-depth-of-binary-treeLeetCode
- Leetcode 298 Binary Tree Longest Consecutive SequenceLeetCode
- LeetCode 104. Maximum Depth of Binary TreeLeetCode
- LeetCode 98. Validate Binary Search TreeLeetCode
- LeetCode#110.Balanced Binary Tree(Tree/Height/DFS/Recursion)LeetCode
- leetcode-124-Binary Tree Maximum Path SumLeetCode
- [LeetCode] 501. Find Mode in Binary Search TreeLeetCode
- [LeetCode] 671. Second Minimum Node In a Binary TreeLeetCode
- LeetCode 124. Binary Tree Maximum Path SumLeetCode
- [leetcode]convert-sorted-array-to-binary-search-treeLeetCode
- LeetCode之Construct String from Binary Tree(Kotlin)LeetCodeStructKotlin
- LeetCode 501. Find Mode in Binary Search TreeLeetCode
- [LeetCode] 2196. Create Binary Tree From DescriptionsLeetCode
- LeetCode C++ 968. Binary Tree Cameras【Tree/DFS】困難LeetCodeC++
- [LeetCode] 109. Convert Sorted List to Binary Search TreeLeetCode