mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #590 from hailincai/master
problems/0102, add Java implementation for leetcode 515
This commit is contained in:
@ -1046,6 +1046,31 @@ class Solution:
|
|||||||
out_list.append(max(in_list))
|
out_list.append(max(in_list))
|
||||||
return out_list
|
return out_list
|
||||||
```
|
```
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
go:
|
go:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user