515. 在每个树行中找最大值

515. Find Largest Value in Each Tree Row

题目描述

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

您需要在二叉树的每一行中找到最大的值。

LeetCode515. Find Largest Value in Each Tree Row

Example:

Input:
```
1
/ \
3 2
/ \ \
5 3 9
```
Output: [1, 3, 9]

Java 实现

TreeNode Class

class TreeNode {
int val;
TreeNode left;
TreeNode right; TreeNode(int x) {
val = x;
}
}
import java.util.LinkedList;
import java.util.List; class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new LinkedList<>();
helper(root, res, 0);
return res;
} public void helper(TreeNode root, List<Integer> res, int depth) {
if (root == null) {
return;
}
if (depth == res.size()) {
res.add(root.val);
} else {
res.set(depth, Math.max(res.get(depth), root.val));
}
helper(root.left, res, depth + 1);
helper(root.right, res, depth + 1);
}
}

参考资料

LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)的更多相关文章

  1. [Swift]LeetCode515. 在每个树行中找最大值 | Find Largest Value in Each Tree Row

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

  2. Java实现 LeetCode 515 在每个树行中找最大值

    515. 在每个树行中找最大值 您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [1, 3, 9] /** * Definition for ...

  3. Leetcode 515. 在每个树行中找最大值

    题目链接 https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/description/ 题目描述 您需要在二叉树的 ...

  4. Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...

  5. 515 Find Largest Value in Each Tree Row 在每个树行中找最大值

    在二叉树的每一行中找到最大的值.示例:输入:           1         /  \        3   2       /  \    \        5   3    9 输出: [ ...

  6. C语言递归之在每个树行中找最大值

    题目描述 您需要在二叉树的每一行中找到最大的值. 示例 输入: / \ / \ \ 输出: [, , ] 题目要求 /** * Definition for a binary tree node. * ...

  7. Leetcode515. Find Largest Value in Each Tree Row在每个树行中找最大值

    您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [1, 3, 9] class Solution { public: vector<i ...

  8. LN : leetcode 515 Find Largest Value in Each Tree Row

    lc 515 Find Largest Value in Each Tree Row 515 Find Largest Value in Each Tree Row You need to find ...

  9. [LeetCode] Find Largest Value in Each Tree Row 找树每行最大的结点值

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

随机推荐

  1. python find和index的区别

    如果找不到目标元素,index会报错,find会返回-1 >>> s="hello world" >>> s.find("llo&qu ...

  2. Docker hello workd

    Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序. 输出Hello world runoob@runoob:~$ docker run ubunt ...

  3. 深度学习面试题16:小卷积核级联卷积VS大卷积核卷积

    目录 感受野 多个小卷积核连续卷积和单个大卷积核卷积的作用相同 小卷积核的优势 参考资料 感受野 在卷积神经网络中,感受野(Receptive Field)的定义是卷积神经网络每一层输出的特征图(fe ...

  4. 深入理解JS中&&和||

    写了这么多JS,才发现JS的语法既是属于C语系的,又与一般C语系的编程语言某些地方有很大区别,其中&&和||就是其中一例. C语系中的&&和|| C语系的&&a ...

  5. (转)darknet 训练心得

    1. 安装darknet 使用Git克隆源码 git clone https://github.com/pjreddie/darknet 我们可能需要修改Makefile,主要修改前三行,配置使用GP ...

  6. Visual Studio 2019更新到16.2.1

    Visual Studio 2019更新到16.2.1   此次更新,包含以下修改: (1)支持Xcode 10.3. (2)修复了Forms项目中,预览Android界面效果bug. (3)修复am ...

  7. 将移远通信的EC20驱动移植到NUC972上(转)

    源: 将移远通信的EC20驱动移植到NUC972上

  8. vue的vuex在使用...mapState 和...mapGetter报错的解决方案

    from:https://blog.csdn.net/ysterling/article/details/88145765 采用mapState 和mapGetter,在页面使用时报错,这是因为bab ...

  9. Dart静态方法、对象操作符和类的继承

    /* Dart中的静态成员: 1.使用static 关键字来实现类级别的变量和函数 2.静态方法不能访问非静态成员,非静态方法可以访问静态成员 */ // class Person { // stat ...

  10. (待续)【转载】 DeepMind发Nature子刊:通过元强化学习重新理解多巴胺

    原文地址: http://www.dataguru.cn/article-13548-1.html -------------------------------------------------- ...