响应式网站解决方案,最好的免费cms系统,服装设计自学零基础,广州有哪些大公司描述 :
给定一棵二叉树的根节点 root #xff0c;请找出该二叉树中每一层的最大值。
题目 :
LeetCode 在每个树行中找最大值 :
515. 在每个树行中找最大值 分析 :
这里其实就是在得到一层之后使用一个变量来记录当前得到的最大值 , 懂了前面的几道这就是小菜
解析 :
/…描述 :
给定一棵二叉树的根节点 root 请找出该二叉树中每一层的最大值。
题目 :
LeetCode 在每个树行中找最大值 :
515. 在每个树行中找最大值 分析 :
这里其实就是在得到一层之后使用一个变量来记录当前得到的最大值 , 懂了前面的几道这就是小菜
解析 :
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val val;* this.left left;* this.right right;* }* }*/
class Solution {public ListInteger largestValues(TreeNode root) {ListInteger list new ArrayList();if(root null){return list;}QueueTreeNode queue new LinkedList();queue.add(root);while(!queue.isEmpty()){int x Integer.MIN_VALUE;int size queue.size();for(int i 0;i size ;i){TreeNode temp queue.remove();x Math.max(temp.val,x);if(temp.left ! null){queue.add(temp.left);}if(temp.right ! null){queue.add(temp.right);}}list.add(x);} return list;}
}