Leetcode 515 Find Largest Value in Each Tree Row
来源:互联网 发布:怎么识谱古筝知乎 编辑:程序博客网 时间:2024/05/21 19:34
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]
DFS的问题,采用pre-order,depth来记录深度
/** * 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<>(); dfs(root, result, 0); return result; } private void dfs(TreeNode root, List<Integer> result, int depth){ if(root == null){ return; } if(result.size() == depth){ result.add(root.val); }else{ result.set(depth, Math.max(result.get(depth), root.val)); } dfs(root.left, result, depth + 1); dfs(root.right, result, depth + 1); }}
阅读全文
0 0
- [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 C++
- Leetcode 515 Find Largest Value in Each Tree Row
- LeetCode#515 Find Largest Value in Each Tree Row (week15)
- LeetCode 515. Find Largest Value in Each Tree Row
- LeetCode OJ 515. Find Largest Value in Each Tree Row
- 【LEETCODE】 Find Largest Value in Each Tree Row javascript实现
- 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
- 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
- 【LeetCode】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
- 查找整型数组的元素之和最大的子串
- spring4.xml配置样例
- DBoW2 回环检测/重定位 算法解析
- 接口测试用例模板
- php中try catch捕获异常实例详解
- Leetcode 515 Find Largest Value in Each Tree Row
- 关于命令行解释器shell的管道的理解
- 关于sort函数的几种用法
- kafka helloworld
- maven简单项目转换Web项目,在properties里没有出现Project Facets
- httpsession 内容整理
- java 缓存 ConcurrentHashMap 单例 简单示例
- linux redis的安装 与使用
- 5.JavaScript深入之作用域链