题目描述

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

示例

输入: 

         / \

       / \   \  

输出: [, , ]

题目要求

 /**
* 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. 调整swt table的行高

    table.addListener(SWT.MeasureItem, new Listener() { public void handleEvent(Event event) { // 设置行高度 ...

  2. winfrom 遍历文本框

    //winform中Control是所有组件的基类 ; i <= ; i++) { Control[] coltxtSpeed = this.Controls.Find("txtbox ...

  3. resize([[data],fn]) 当调整浏览器窗口的大小时,发生 resize 事件。

    resize([[data],fn]) 概述 当调整浏览器窗口的大小时,发生 resize 事件.   参数 fnFunctionV1.0 在每一个匹配元素的resize事件中绑定的处理函数.直线电机 ...

  4. react 后台(一)react + redux + react-route + webpack+ axios + antd+styled-components(替代less)

    create-react-app my-admin 项目技术栈 react + redux + react-route + webpack+ axios + antd+styled-component ...

  5. IDEA工具的安装、破解与配置

    一.什么是IDEA? IDEA 全称 IntelliJ IDEA,是java编程语言开发的集成环境,是目前最好用的java集成开发工具.他最突出的功能是调试(Debug),可以对Java代码,Java ...

  6. [Luogu] 矩形覆盖

    https://www.luogu.org/problemnew/show/P1034 数据太水 爆搜过掉 #include <iostream> #include <cstdio& ...

  7. SpringMVC框架下Web项目的搭建与部署

    这篇文章已被废弃. 现在,Deolin使用Maven构建项目,而不是下载Jar文件,使用Jetty插件调试项目,而不是外部启动Tomcat. SpringMVC比起Servlet/JSP方便了太多 W ...

  8. OpenGL+VS2010环境配置及遇到的问题

    OpenGL+VS2010+GLUT工具包+WIN10系统: 第一步,安装GLUT工具包 Windows环境下的GLUT下载地址:(大小约为150k) http://www.opengl.org/re ...

  9. CRT小键盘输入乱码

    Options --> Session Options --> Terminal --> Emulation --> Modes 去选中 Enable keypad mode ...

  10. UVALive 4726 Average ——(斜率优化DP)

    这是第一次写斜率优化DP= =.具体的做法参照周源论文<浅谈数形结合思想在信息学竞赛中的应用>.这里仅提供一下AC的代码. 有两点值得注意:1.我这个队列的front和back都是闭区间的 ...