当前位置:网站首页>The sword refers to Offer II 044. The maximum value of each level of the binary tree-dfs method
The sword refers to Offer II 044. The maximum value of each level of the binary tree-dfs method
2022-08-03 19:42:00 【Mr Gao】
剑指 Offer II 044. 二叉树每层的最大值
给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值.
示例1:
输入: root = [1,3,2,5,3,null,9]
输出: [1,3,9]
解释:
1
/
3 2
/ \ \
5 3 9
示例2:
输入: root = [1,2,3]
输出: [1,3]
解释:
1
/
2 3
示例3:
输入: root = [1]
输出: [1]
示例4:
输入: root = [1,null,2]
输出: [1,2]
解释:
1
2
示例5:
输入: root = []
输出: []
/** * 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 f(struct TreeNode* root){
if(root){
int a=f(root->left)+1;
int b=f(root->right)+1;
if(a>b){
return a;
}
else{
return b;
}
}
else{
return 0;
}
}
void f2(struct TreeNode* root,int h,int *a){
if(root){
f2(root->left,h+1,a);
f2(root->right,h+1,a);
if(root->val>a[h]){
a[h]=root->val;
}
}
}
int* largestValues(struct TreeNode* root, int* returnSize){
int h=f(root);
int *re=(int *)malloc(sizeof(int)*h);
* returnSize=h;
for(int i=0;i<h;i++){
re[i]=-2147483648;
}
f2(root,0,re);
return re;
}
边栏推荐
- CC2530_ZigBee+华为云IOT:设计一套属于自己的冷链采集系统
- tensorflow-gpu2.4.1安装配置详细步骤
- 揭秘5名运维如何轻松管理数亿级流量系统
- Solution for no navigation bar after Word is saved as PDF
- 基于DMS的数仓智能运维服务,知多少?
- 1161 最大层内元素和——Leetcode天天刷【BFS】(2022.7.31)
- List类的超详细解析!(超2w+字)
- Reveal how the five operational management level of hundreds of millions of easily flow system
- 盘点在线帮助中心对企业能够起到的作用
- 小马智行起诉擎天智卡:索赔6000万 彭军称要斗争到底
猜你喜欢

node版本切换工具NVM以及npm源管理器nrm

高性能计算软件与开源生态| ChinaOSC

国产虚拟化云宏CNware WinStack安装体验-5 开启集群HA

危化企业双重预防机制数字化建设进入全面实施阶段

Matlab论文插图绘制模板第42期—气泡矩阵图(相关系数矩阵图)

Shell programming loop statement

FreeRTOS Intermediate

【统计机器学习】线性回归模型

【leetcode】剑指 Offer II 009. 乘积小于 K 的子数组(滑动窗口、双指针)

Benchmarking Lane-changing Decision-making for Deep Reinforcement Learning
随机推荐
1161 最大层内元素和——Leetcode天天刷【BFS】(2022.7.31)
Postgresql快照优化Globalvis新体系分析(性能大幅增强)
net-snmp编译报错:/usr/bin/ld: cannot find crti.o: No such file or directory
Redis 内存满了怎么办?这样置才正确!
宁德时代2号人物黄世霖辞任副董事长:身价1370亿
如何理解即时通讯开发移动网络的“弱”和“慢”
力扣刷题之合并两个有序数组
揭秘5名运维如何轻松管理数亿级流量系统
The effective square of the test (one question of the day 7/29)
阿里巴巴政委体系-第八章、阿里政委工作方法论
高性能计算软件与开源生态| ChinaOSC
基于移动GIS的环保生态管理系统
【统计机器学习】线性回归模型
LeetCode 952. Calculate Maximum Component Size by Common Factor
Postgresql-xl全局快照与GTM代码走读(支线)
Execute the mysql script file in the docker mysql container and solve the garbled characters
Shell programming loop statement
622 设计循环队列——Leetcode天天刷【循环队列,数组模拟,双指针】(2022.8.2)
glide set gif start stop
单调栈及其应用