【LeetCode從零單排】No102 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 binary tree
* 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>> al=new ArrayList<List<Integer>>();
levelHelper(al,root,0);
return al;
}
public void levelHelper(List<List<Integer>> res,TreeNode root,int height){
if(root==null) return;
if(height>=res.size()){
res.add(new ArrayList<Integer>());
}
res.get(height).add(root.val);
levelHelper(res,root.left,height+1);
levelHelper(res,root.right,height+1);
}
}
/********************************
* 本文來自部落格 “李博Garvin“
* 轉載請標明出處:http://blog.csdn.net/buptgshengod
******************************************/
相關文章
- 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解題報告 102. Binary Tree Level Order Traversal [easy]LeetCode
- [LintCode] Binary Tree Level Order
- 【LeetCode從零單排】No104 Maximum Depth of Binary TreeLeetCode
- leetcode - Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Inorder TraversalLeetCode
- Leetcode Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Postorder TraversalLeetCode
- 【LeetCode從零單排】No 114 Flatten Binary Tree to Linked ListLeetCode
- [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 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從零單排】No96 Unique Binary Search TreesLeetCode
- Leetcode-145題:Binary Tree Postorder TraversalLeetCode