LeetCode Find Largest Value in Each Tree Row
2017-11-27 13:09
465 查看
原题链接在这里:https://leetcode.com/problems/find-largest-value-in-each-tree-row/description/
题目:
You need to find the largest value in each row of a binary tree.
Example:
题解:
可以采用BFS, 类似Binary Tree Level Order Traversal. 每层算最大值加入res中.
Time Complexity: O(n).
Space: O(n).
AC Java:
也可以DFS. 用depth来标记res中的index位置.
Time Complexity: O(n).
Space: O(logn). stack space.
AC Java:
题目:
You need to find the largest value in each row of a binary tree.
Example:
Input: 1 / \ 3 2 / \ \ 5 3 9 Output: [1, 3, 9]
题解:
可以采用BFS, 类似Binary Tree Level Order Traversal. 每层算最大值加入res中.
Time Complexity: O(n).
Space: O(n).
AC Java:
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 class Solution { 11 public List<Integer> largestValues(TreeNode root) { 12 List<Integer> res = new ArrayList<Integer>(); 13 if(root == null){ 14 return res; 15 } 16 17 LinkedList<TreeNode> que = new LinkedList<TreeNode>(); 18 que.add(root); 19 int curCount = 1; 20 int nextCount = 0; 21 int curLevelMax = Integer.MIN_VALUE; 22 while(!que.isEmpty()){ 23 TreeNode cur = que.poll(); 24 curCount--; 25 curLevelMax = Math.max(curLevelMax, cur.val); 26 27 if(cur.left != null){ 28 que.add(cur.left); 29 nextCount++; 30 } 31 32 if(cur.right != null){ 33 que.add(cur.right); 34 nextCount++; 35 } 36 37 if(curCount == 0){ 38 curCount = nextCount; 39 nextCount = 0; 40 res.add(curLevelMax); 41 curLevelMax = Integer.MIN_VALUE; 42 } 43 } 44 return res; 45 } 46 }
也可以DFS. 用depth来标记res中的index位置.
Time Complexity: O(n).
Space: O(logn). stack space.
AC Java:
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 class Solution { 11 public List<Integer> largestValues(TreeNode root) { 12 List<Integer> res = new ArrayList<Integer>(); 13 if(root == null){ 14 return res; 15 } 16 17 dfs(root, res, 0); 18 return res; 19 } 20 21 private void dfs(TreeNode root, List<Integer> res, int depth){ 22 if(root == null){ 23 return; 24 } 25 26 if(depth == res.size()){ 27 // 之前没有的碰到的深度. 28 res.add(root.val); 29 }else{ 30 // 之前有平级的深度. 31 res.set(depth, Math.max(res.get(depth), root.val)); 32 } 33 34 dfs(root.left, res, depth+1); 35 dfs(root.right, res, depth+1); 36 } 37 }
相关文章推荐
- LeetCode--Find Largest Value in Each Tree Row
- LeetCode: Find Largest Value in Each Tree Row
- 【LEETCODE】 Find Largest Value in Each Tree Row javascript实现
- [LeetCode] Find Largest Value in Each Tree Row 找树每行最大的结点值
- [LeetCode]515 Find Largest Value in Each Tree Row(dfs)
- LeetCode——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(二叉树层序遍历)
- 【LeetCode515 Find Largest Value in Each Tree Row】二叉树每行的最大值
- 【LeetCode】Find Largest Value in Each Tree Row 解题报告
- leetcode515 Find Largest Value In Each Tree Row Java
- LeetCode:Find Largest Value in Each Tree Row
- 算法分析与设计丨第三周丨LeetCode(6)——Find Largest Value in Each Tree Row(Medium)
- LeetCode之Find Largest Value in Each Tree Row
- [leetcode 515]Find Largest Value in Each Tree Row
- Leetcode 515 Find Largest Value in Each Tree Row
- leetcode 515 Find Largest Value in Each Tree Row C++
- LeetCode算法问题17 —— Find Largest Value in Each Tree Row
- Medium:Find Largest Value in Each Tree Row
- Tree——Find Largest Value in Each Tree Row