https://leetcode.com/problems/find-largest-value-in-each-tree-row/
https://discuss.leetcode.com/topic/78945/java-bfs
https://discuss.leetcode.com/topic/78955/verbose-java-solution-binary-tree-level-order-traversal-again
https://discuss.leetcode.com/topic/79178/9ms-java-dfs-solution
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]X. BFS
https://discuss.leetcode.com/topic/78945/java-bfs
https://discuss.leetcode.com/topic/78955/verbose-java-solution-binary-tree-level-order-traversal-again
public int[] findValueMostElement(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
List<Integer> res = new ArrayList<Integer>();
queue.add(root);
int queueSize = root == null ? 0 : 1;
while (queueSize > 0) {
int largestElement = Integer.MIN_VALUE;
for (int i=0;i<queueSize;i++) {
TreeNode cur = queue.poll();
largestElement = Math.max(cur.val, largestElement);
if (cur.left != null) queue.add(cur.left);
if (cur.right != null) queue.add(cur.right);
}
res.add(largestElement);
queueSize = queue.size();
}
int[] resArray = new int[res.size()];
for (int i=0;i<res.size();i++) resArray[i] = res.get(i);
return resArray;
}
X. DFShttps://discuss.leetcode.com/topic/79178/9ms-java-dfs-solution
Just a simple pre-order traverse idea. Use depth to expand result list size and put the max value in the appropriate position.
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
helper(root, res, 0);
return res;
}
private void helper(TreeNode root, List<Integer> res, int d){
if(root == null){
return;
}
//expand list size
if(d == res.size()){
res.add(root.val);
}
else{
//or set value
res.set(d, Math.max(res.get(d), root.val));
}
helper(root.left, res, d+1);
helper(root.right, res, d+1);
}