diff --git a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.java b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.java new file mode 100644 index 0000000000000..a4cfba5de4536 --- /dev/null +++ b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.java @@ -0,0 +1,35 @@ +/** + * 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 largestValues(TreeNode root) { + List list = new ArrayList<>(); + dfs(list, root, 0); + return list; + } + + private void dfs(List list, TreeNode root, int level) { + if (root == null) { + return; + } + // 每深入一层,先把那一层的第一个节点加入返回 list中 + if (list.size() == level) { + list.add(root.val); + } + // 此时 size > level ,那么就是开始遍历每一层 的 其他节点(不包括最左边的节点), + // 直接比较list的对应下标(index)的值与当前值就好 + else { + list.set(level, Math.max(list.get(level), root.val)); + } + // 左右子树,深度要+1 + dfs(list, root.left, level + 1); + dfs(list, root.right, level + 1); + } +} \ No newline at end of file