515. 在每个树行中找最大值
生活随笔
收集整理的这篇文章主要介绍了
515. 在每个树行中找最大值
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
您需要在二叉树的每一行中找到最大的值。
示例:
输入: 1/ \3 2/ \ \ 5 3 9 输出: [1, 3, 9] 在真实的面试中遇到过这道题? class Solution {public List<Integer> largestValues(TreeNode root) {List<Integer> res = new ArrayList<>();Queue<TreeNode> queue = new LinkedList<>();if(root == null) return res;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();if(node.val > Max) Max = node.val;if(node.left != null) queue.add(node.left);if(node.right != null) queue.add(node.right);}res.add(Max);}return res;} }
转载于:https://www.cnblogs.com/Roni-i/p/10510237.html
总结
以上是生活随笔为你收集整理的515. 在每个树行中找最大值的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: T-SQL像数组一样处理字符串、分割字符
- 下一篇: [BZOJ1297/Luogu4159]