diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index c38cc796..007eac44 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1046,6 +1046,31 @@ class Solution: out_list.append(max(in_list)) return out_list ``` +java代码: + +```java +class Solution { + public List largestValues(TreeNode root) { + List retVal = new ArrayList(); + Queue tmpQueue = new LinkedList(); + if (root != null) tmpQueue.add(root); + + while (tmpQueue.size() != 0){ + int size = tmpQueue.size(); + List lvlVals = new ArrayList(); + 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; + } +} +``` go: