515. Find Largest Value in Each Tree Row
来源:互联网 发布:默纳克解密软件 编辑:程序博客网 时间:2024/06/04 17:56
You need to find the largest value in each row of a binary tree.
Example:
Input: 1 / \ 3 2 / \ \ 5 3 9 Output: [1, 3, 9]
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<Integer> largestValues(TreeNode root) { List<Integer> result = new ArrayList<Integer>(); if(root == null){ return result; } Queue<TreeNode> q = new LinkedList<TreeNode>(); q.offer(root); while(!q.isEmpty()){ int size = q.size(); int max = Integer.MIN_VALUE; for(int i=0; i<size; i++){ TreeNode cur = q.poll(); max = Math.max(cur.val, max); if(cur.left != null){ q.offer(cur.left); } if(cur.right != null){ q.offer(cur.right); } } result.add(max); } return result; }}
阅读全文
0 0
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- LeetCode 515. Find Largest Value in Each Tree Row
- 515. Find Largest Value in Each Tree Row
- LeetCode OJ 515. Find Largest Value in Each Tree Row
- 515. Find Largest Value in Each Tree Row
- 515. Find Largest Value in Each Tree Row
- week3-NO.515. Find Largest Value in Each Tree Row
- LeetCode笔记:515. Find Largest Value in Each Tree Row
- [leetcode] 515. Find Largest Value in Each Tree Row
- 【LeetCode】515. Find Largest Value in Each Tree Row
- 个人总结:关于c4droid导出apk时的资源填写路径
- Linux 常用命令
- response.setContentType与 request.setCharacterEncoding 区别
- PS制作一个百度的小熊掌LOGO
- Eclipse控制台输出信息的控制
- 515. Find Largest Value in Each Tree Row
- /var/spool/cron/和/etc/crontab的区别
- sizeof(变量名)与strlen(字符数组名)的使用区别
- 信道容量
- mysql-5.7.18Installer安装版下载与安装
- 正则中的/num用法
- SQL 游标
- Linux 文件夹下所有文件指定字符串替换
- 08:石头剪刀布