Find Largest Value in Each Tree Row
2017-02-17 19:13
225 查看
You need to find the largest value in each row of a binary tree.
Example:
Example:
Input: 1 / \ 3 2 / \ \ 5 3 9 Output: [1, 3, 9]
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public List<Integer> largestValues(TreeNode root) { 12 List<Integer> result = new ArrayList<Integer>(); 13 helper(root, result, 0); 14 return result; 15 } 16 17 private void helper(TreeNode root, List<Integer> result,int level) { 18 if (root == null) return; 19 20 if (result.size() < level + 1) { 21 result.add(new Integer(Integer.MIN_VALUE)); 22 } 23 result.set(level, Math.max(result.get(level), root.val)); 24 25 helper(root.left, result, level + 1); 26 helper(root.right, result, level + 1); 27 } 28 }
相关文章推荐
- leetcode 515 Find Largest Value in Each Tree Row C++
- LeetCode之Find Largest Value in Each Tree Row
- LeetCode.515 Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- 第十二周算法分析与设计: Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- LeetCode: Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row问题及解法
- Week Training: 515 Find Largest Value in Each Tree Row
- LeetCode 515 Find Largest Value in Each Tree Row(二叉树层序遍历)
- leetcode515: Find Largest Value in Each Tree Row
- LeetCode之Find Largest Value in Each Tree Row(Kotlin)
- [leetcode 515]Find Largest Value in Each Tree Row
- LeetCode#515 Find Largest Value in Each Tree Row (week15)
- Leetcode 515 Find Largest Value in Each Tree Row
- LeetCode:Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- LeetCode#515 Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Medium:Find Largest Value in Each Tree Row