diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 0f9b2df659..d0f584fe04 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -1072,6 +1072,32 @@ class Solution { } ``` +```java +//方法二:用一个max变量来保存最大值 +class Solution { + public List largestValues(TreeNode root) { + Queue queue = new LinkedList(); + List result = new ArrayList<>(); + 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++){ + TreeNode node = queue.poll(); + max = Math.max(node.val, max); + if(node.left != null) + queue.add(node.left); + if(node.right != null) + queue.add(node.right); + } + result.add(max); // 取最大值放进数组 + } + return result; + } +} +``` + go: ```GO