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

  1. class TreeNode {
  2. int val;
  3. TreeNode left;
  4. TreeNode right;
  5. TreeNode(int x) {
  6. val = x;
  7. }
  8. }
  1. import java.util.LinkedList;
  2. import java.util.List;
  3. class Solution {
  4. public List<Integer> largestValues(TreeNode root) {
  5. List<Integer> res = new LinkedList<>();
  6. helper(root, res, 0);
  7. return res;
  8. }
  9. public void helper(TreeNode root, List<Integer> res, int depth) {
  10. if (root == null) {
  11. return;
  12. }
  13. if (depth == res.size()) {
  14. res.add(root.val);
  15. } else {
  16. res.set(depth, Math.max(res.get(depth), root.val));
  17. }
  18. helper(root.left, res, depth + 1);
  19. helper(root.right, res, depth + 1);
  20. }
  21. }

参考资料

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. shell脚本编程基础介绍

    Linux系统——shell脚本编程基础介绍 1.什么是shell 它是一个命令解释器,在linux/unix操作系统的最外层,负责直接与用户对话,把用户的输入解释给操作系统,并处理各种操作输出的结果 ...

  2. meshing-做类似ICEM的o型剖分

    原版视频下载地址: https://pan.baidu.com/s/1nvxD0jn 密码: db42

  3. Spring mybatis thymeleaf 基础操作,实现数据展示,修改,删除,查询

    目录结构如图 index.html <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thyme ...

  4. CentOS下载与服务器版安装(VMware)

    1. 下载 首先需要选择一个版本,因为华为云最新只提供了CentOS 7.6,所以要选择CentOS 7版本的. 官网只提供了最新的版本,而且服务器在国外,下载速度贼慢. 不过官方提供了分布在各个地区 ...

  5. 【转】JVM类装载机制的解析,热更新的探讨(二)

    同样,一个Class对象必须知道自己的超类.超级接口.因此,Class对象会引用自己的超类和超级接口的Class对象.这种引用一定是实例引用.(实际上,超类.超级接口的引用也存储在常量池中,但为了区分 ...

  6. PostgreSQL学习笔记(九) 用户、角色、权限管理

    PostgreSQL是一个多用户数据库,可以为不同用户指定允许的权限. 角色PostgreSQL使用角色的概念管理数据库访问权限. 根据角色自身的设置不同,一个角色可以看做是一个数据库用户,或者一组数 ...

  7. sqlite3 查看Cookie文件<转>

    原帖地址:https://blog.csdn.net/yygydjkthh/article/details/37604309 .使用命令对此数据库进行打开: $ sqlite3 cookies.sql ...

  8. 范围指示器Extent Indicators

    范围指示器Extent Indicators 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.com   商务合作,科技咨询,版权转让:向日葵,135- ...

  9. darknet 的python接口使用

    首先,python接口文件在安装好的darknet目录下的python文件夹,打开就可以看到 这里的darknet.py文件就是python接口 用编辑器打开查看最后部分代码: 使用十分简单,先将网络 ...

  10. linux内核是在哪里创建1号进程的?

    1. 请看rest_init的完整代码(不看也没关系,内核版本为5.2, init/main.c) noinline void __ref rest_init(void) { struct task_ ...