当前位置:网站首页>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;
}
边栏推荐
- 建模该从哪一步开始?给你分析,给零基础的你一些学习建议
- Postgresql-xl global snapshot and GTM code walking (branch line)
- 宁德时代2号人物黄世霖辞任副董事长:身价1370亿
- 友宏医疗与Actxa签署Pre-M Diabetes TM 战略合作协议
- Shell programming loop statement
- 力扣刷题之爬楼梯(7/30)
- 国产虚拟化云宏CNware WinStack安装体验-5 开启集群HA
- WPF .cs中使用资源文件中的ControlTemplate或Style并找到控件
- Postgresql源码(65)新快照体系Globalvis工作原理分析
- Postgresql源码(64)查询执行——子模块Executor(2)执行前的数据结构和执行过程
猜你喜欢
随机推荐
622 设计循环队列——Leetcode天天刷【循环队列,数组模拟,双指针】(2022.8.2)
LeetCode 952. Calculate Maximum Component Size by Common Factor
Postgresql源码(64)查询执行——子模块Executor(2)执行前的数据结构和执行过程
阿里巴巴政委体系-第七章、阿里政委培育
Power button brush the topic of merging two orderly array
Matlab论文插图绘制模板第42期—气泡矩阵图(相关系数矩阵图)
开源教育论坛| ChinaOSC
Compose原理-compose中是如何实现事件分法的
力扣刷题之爬楼梯(7/30)
基于移动GIS的环保生态管理系统
relocation R_X86_64_PC32 against,/usr/bin/ld: final link failed: Bad value
JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof
LeetCode 622. Designing Circular Queues
149. The largest number on a straight line, and check the set
傅里叶变换(深入浅出)
Climbing Stairs (7/30)
awk语法-02-运算、数组、格式化输出
虚拟机vmware设置nat模式上网
力扣解法汇总899-有序队列
宁德时代2号人物黄世霖辞任副董事长:身价1370亿