Skip to content

Commit

Permalink
Merge pull request #1148 from gowsp/patch-1
Browse files Browse the repository at this point in the history
更新每个树行中找最大值Java实现
  • Loading branch information
youngyangyang04 authored Mar 28, 2022
2 parents 79b2453 + b79f3e0 commit 62e4681
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions problems/0102.二叉树的层序遍历.md
Original file line number Diff line number Diff line change
Expand Up @@ -1287,23 +1287,23 @@ java代码:
```java
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> retVal = new ArrayList<Integer>();
Queue<TreeNode> tmpQueue = new LinkedList<TreeNode>();
if (root != null) tmpQueue.add(root);
while (tmpQueue.size() != 0){
int size = tmpQueue.size();
List<Integer> lvlVals = new ArrayList<Integer>();
for (int index = 0; index < size; index++){
TreeNode node = tmpQueue.poll();
lvlVals.add(node.val);
if (node.left != null) tmpQueue.add(node.left);
if (node.right != null) tmpQueue.add(node.right);
}
retVal.add(Collections.max(lvlVals));
}

return retVal;
if(root == null){
return Collections.emptyList();
}
List<Integer> result = new ArrayList();
Queue<TreeNode> queue = new LinkedList();
queue.offer(root);
while(!queue.isEmpty()){
int max = Integer.MIN_VALUE;
for(int i = queue.size(); i > 0; i--){
TreeNode node = queue.poll();
max = Math.max(max, node.val);
if(node.left != null) queue.offer(node.left);
if(node.right != null) queue.offer(node.right);
}
result.add(max);
}
return result;
}
}
```
Expand Down

0 comments on commit 62e4681

Please sign in to comment.