leetcode102:Binary Tree Level Order Traversal
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,#,#,15,7},
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
//先摘抄一下別人的程式碼
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> resultList = new ArrayList<List<Integer>>();//resultlist是陣列連結串列
if (root == null) {
return resultList;
}
List<Integer> levelStorage = new LinkedList<Integer>();//levelStorage是resultlist的代替者
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();//queue是連結串列
queue.offer(root);//進佇列
queue.offer(null);
while (queue.size() > 1) {
TreeNode top = queue.poll();//出佇列
if (top == null) {
resultList.add(levelStorage);
queue.offer(null);
levelStorage = new LinkedList<Integer>();
} else {
levelStorage.add(top.val);
if (top.left != null) {
queue.offer(top.left);
}
if (top.right != null) {
queue.offer(top.right);
}
}
}
resultList.add(levelStorage);
return resultList;
}
}
相關文章
- 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
- [LeetCode]107. Binary Tree Level Order Traversal IILeetCode
- [LintCode] Binary Tree Level Order
- LeetCode-Binary Tree Vertical Order TraversalLeetCode
- LeetCode解題報告 102. Binary Tree Level Order Traversal [easy]LeetCode
- 【LeetCode從零單排】No102 Binary Tree Level Order TraversalLeetCode
- 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
- Construct Binary Tree from Preorder and Inorder TraversalStruct
- Leetcode-Binary Tree Postorder TraversalLeetCode
- Leetcode-Binary Tree Inorder TraversalLeetCode
- Construct Binary Tree from Inorder and Postorder TraversalStruct
- 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
- Construct Binary Tree from Inorder and Postorder Traversal Traversal leetcode javaStructLeetCodeJava
- Leetcode 94. Binary Tree Inorder TraversalLeetCode
- 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-145題:Binary Tree Postorder TraversalLeetCode
- leetcode [python] 【144】 Binary Tree Preorder TraversalLeetCodePython