题目描述

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

示例

输入: 

         / \

       / \   \  

输出: [, , ]

题目要求

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/ /**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* largestValues(struct TreeNode* root, int* returnSize){ }

题解

 int max(int a,int b){
return a>b?a:b;
} int work(struct TreeNode* r, int* ar, int f){
ar[f]=max(ar[f],r->val);
if(r->left==NULL&&r->right==NULL)return f+;
else if(r->left==NULL)return work(r->right,ar,f+);
else if(r->right==NULL)return work(r->left,ar,f+);
else return max(work(r->left,ar,f+),work(r->right,ar,f+));
} int* largestValues(struct TreeNode* root, int* returnSize){
int *array=(int *)malloc(*sizeof(int));
for(int i=;i<;i++)array[i]=-;
if(root==NULL){
*returnSize=;
return array;
}
*returnSize=work(root,array,);
return array;
}

1.递归

这道题用BFS逻辑比较简单但是需要消耗大量内存去存储节点值,我最近在学习DFS,所以就用DFS实现。

这道题用DFS思路,切入点不太好想出来。

分析逻辑是每搜索到一个节点,就将其节点值与返回数组对应位置的值进行比较,若对应位置无值则直接插入,若对应位置有值则填入更大者,对应位置的下标即是节点的深度。

因此只要用深搜的思路遍历每一个节点,遍历携带参数为节点深度,就可以用时间复杂度为O(n)解决此问题。

2.非指针变量的值

这道题一开始我遇到了一些困惑

C语言递归之在每个树行中找最大值的更多相关文章

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

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

  2. LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    515. 在每个树行中找最大值 515. Find Largest Value in Each Tree Row 题目描述 You need to find the largest value in ...

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

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

  4. [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 ...

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

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

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

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

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

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

  8. 算法整理(php语言完成),持续更行中......

    一下所有实例中,均在同一个方法中,所以算法使用内部函数完成 归并排序 public function test1Action () { $tmp = 0; $al_merge = function($ ...

  9. 数据结构-C语言递归实现树的前中后序遍历

    #include <stdio.h> #include <stdlib.h> typedef struct tree { int number ; struct tree *l ...

随机推荐

  1. 关于EZDML数据库表结构制作设计工具使用踩的坑

    我使用的是一款EZDML的数据库表结构制作设计工具 最开始在数据库创建数据库名为personalmall,基字符集为默认,数据库排序规则也是默认,创建完成之后 去EZDML生成SQL 点击执行sql ...

  2. Python中正则匹配使用findall,捕获分组(xxx)和非捕获分组(?:xxx)的差异

    转自:https://blog.csdn.net/qq_42739440/article/details/81117919 下面是我在用findall匹配字符串时遇到的一个坑,分享出来供大家跳坑. 例 ...

  3. VSCode:配置自动修复eslint

    { //"tfvc.location": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Profess ...

  4. CF46F Hercule Poirot Problem

    题意: 有n个房间和m扇门,每扇门有且仅有一把钥匙 有k个人度过了两天,在第一天开始的时候所有的门都是关闭的,在第二天结束的时候,所有的门也都是关闭的 在这两天内,每个人可以执行如下操作若干次: 关上 ...

  5. 树莓派打造mini广播(FM)系统

    树莓派打造mini广播(FM)系统 注意相关法律限制功率大小和频段.,以下只能用于测试目的 github项目: https://github.com/miegl/PiFmAdv 安装: apt-get ...

  6. ios高版本中select的option选项内容不显示问题

    <select class="form-control" @change="inputChange(item.id,postObj[item.id])" ...

  7. openpyxl -用于读/写Excel 2010 XLSX/XLSM文件的python库

    openpyxl -用于读/写Excel 2010 XLSX/XLSM文件的python库¶ https://www.osgeo.cn/openpyxl/index.html

  8. elasticsearch各种服务链接

    查看elasticsearch信息http://localhost:9200/ 查看某个索引信息http://localhost:9200/index_name例如:http://localhost: ...

  9. JAVA基础知识|进程与线程

    一.什么是进程?什么是线程? 操作系统可以同时支持多个程序的运行,而一个程序可以狭义的认为就是一个进程.在一个进程的内部,可能包含多个顺序执行流,而每个执行流就对应一个线程. 1.1.进程 进程:是计 ...

  10. Maven的安装和配置(Windows 10)

    1. 官网下载Maven管理工具 官网:https://maven.apache.org/download.cgi 系统要求: JDK:Maven 3.3以上需要JDK 1.7以上版本支持 Memor ...