当前位置:网站首页>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;
}
边栏推荐
- 那些年我写过的语言
- redis常用命令,HSET,XADD,XREAD,DEL等
- 云图说丨初识华为云微服务引擎CSE
- NNLM、RNNLM等语言模型 实现 下一单词预测(next-word prediction)
- JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof
- 钱江摩托某型号产品ECU货不对版 消费者知情权应如何保障?
- Network protocol-TCP, UDP difference and TCP three-way handshake, four wave
- 揭秘5名运维如何轻松管理数亿级流量系统
- Postgresql快照优化Globalvis新体系分析(性能大幅增强)
- Unity获取canvas 下ui 在屏幕中的实际坐标
猜你喜欢
149. The largest number on a straight line, and check the set
LeetCode 952. 按公因数计算最大组件大小
Shell编程之循环语句
flex布局
花 30 美金请 AI 画家弄了个 logo,网友:画得非常好,下次别画了!
虚拟机vmware设置nat模式上网
详解AST抽象语法树
Introduction to Cosine Distance
云图说丨初识华为云微服务引擎CSE
Reveal how the five operational management level of hundreds of millions of easily flow system
随机推荐
梅科尔工作室-14天华为培训七
net-snmp私有mib动态加载到snmpd
从文本匹配到语义相关——新闻相似度计算的一般思路
2022年最新的Android面试大厂必考174题(附带详细答案)
阿里巴巴政委体系-第九章、阿里政委启示录
C中的数据存储
tensorflow-gpu2.4.1安装配置详细步骤
网络协议-TCP、UDP区别及TCP三次握手、四次挥手
Line the last time the JVM FullGC make didn't sleep all night, collapse
CS免杀姿势
ctfshow php特性
MySQL基础
标准C语言学习总结11
软件测试技术之如何编写测试用例(3)
开源生态研究与实践| ChinaOSC
awk语法-02-运算、数组、格式化输出
MySQL 主从,6 分钟带你掌握!
线上一次JVM FullGC搞得整晚都没睡,彻底崩溃
X86 function call model analysis
告诉你0基础怎么学好游戏建模?