515. Find Largest Value in Each Tree Row

网友投稿 558 2022-09-04

515. Find Largest Value in Each Tree Row

515. Find Largest Value in Each Tree Row

You need to find the largest value in each row of a binary tree.

Example:

Input: \ 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; } * } */class Solution { public List largestValues(TreeNode root) { List res = new ArrayList(); helper(root, res, 0); return res; } private void helper(TreeNode root, List res, int d){ if(root == null){ return; } if(d == res.size()){ res.add(root.val); }else{ res.set(d, Math.max(res.get(d), root.val)); } helper(root.left, res, d + 1); helper(root.right, res, d + 1); }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:基于 Nginx 的 HTTPS 性能优化实践
下一篇:523. Continuous Subarray Sum
相关文章

 发表评论

暂时没有评论,来抢沙发吧~