mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
update 0102.二叉树的层序遍历.md 515java解法二format
This commit is contained in:
@ -1078,16 +1078,17 @@ class Solution {
|
||||
public List<Integer> largestValues(TreeNode root) {
|
||||
Queue<TreeNode> queue = new LinkedList<TreeNode>();
|
||||
List<Integer> result = new ArrayList<>();
|
||||
if (root!=null) queue.add(root);
|
||||
if (root != null) queue.add(root);
|
||||
|
||||
while(!queue.isEmpty()){
|
||||
int size = queue.size();
|
||||
int max = Integer.MIN_VALUE; // 初始化为最小值
|
||||
for(int i=0;i<size;i++){
|
||||
for(int i = 0; i < size; i++){
|
||||
TreeNode node = queue.poll();
|
||||
max = Math.max(node.val,max);
|
||||
if(node.left!=null)
|
||||
max = Math.max(node.val, max);
|
||||
if(node.left != null)
|
||||
queue.add(node.left);
|
||||
if(node.right!=null)
|
||||
if(node.right != null)
|
||||
queue.add(node.right);
|
||||
}
|
||||
result.add(max); // 取最大值放进数组
|
||||
|
Reference in New Issue
Block a user