题目描述

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

示例

输入: 

         / \

       / \   \  

输出: [, , ]

题目要求

 /**
* 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. DataSet,DataTable,DataView、DataRelation

    一.创建Dataset和DataTable DataSet ds = new DataSet();//DataSetName默认为"NewDataSet" DataTable ta ...

  2. selenium web driver

    WebDriver 支持的浏览器 IE6-10 FireFox大部分版本 Chrome Safari Opera Andrioid 系统上的自带浏览器 IOS系统上自带浏览器 HtmlUnit的无界面 ...

  3. SIGAI深度学习第一集 机器学习与数学基础知识

    SIGAI深度学习课程: 本课程全面.系统.深入的讲解深度学习技术.包括深度学习算法的起源与发展历史,自动编码器,受限玻尔兹曼机,卷积神经网络,循环神经网络,生成对抗网络,深度强化学习,以及各种算法的 ...

  4. P2634 [国家集训队]聪聪可可 点分治

    思路:点分治 提交:1次 题解: 不需要什么容斥...接着板子题说: 还是基本思路:对于一颗子树,与之前的子树做贡献. 我们把路径的权值在\(\%3\)意义下分类,即开三个桶\(c[0],c[1],c ...

  5. ora-28002

    1.查看指定概要文件(如default)的密码有效期设置: SELECT * FROM dba_profiles s WHERE s.profile='DEFAULT' AND resource_na ...

  6. 让JPA的Query查询接口返回Map对象

    在JPA 2.0 中我们可以使用entityManager.createNativeQuery()来执行原生的SQL语句. 但当我们查询结果没有对应实体类时,query.getResultList() ...

  7. 第2组 Alpha冲刺(3/4)

    17-材料-黄智(252342126) 22:10:46 队名:十一个憨批 组长博客 作业博客 组长黄智 过去两天完成的任务:写博客,复习C语言 GitHub签入记录 接下来的计划:构思游戏实现 还剩 ...

  8. Send Boxes to Alice

    E. Send Boxes to Alice 首先求出每一个位置的前缀和. 对答案进行复杂度为\(\sqrt{a[n]}\)的遍历,因为最后的答案不可能大于\(\sqrt{a[n]}\) for(ll ...

  9. elasticsearch _update api 更新部分字段内容

    https://www.elastic.co/guide/cn/elasticsearch/guide/current/partial-updates.htmlupdate 请求最简单的一种形式是接收 ...

  10. Embedded based learning

    简单整理了一些嵌入式底层需要接触的相关概念.   # CPU  CU. Control Unit. send need-clac-data -> ALU clac -> get resul ...