当前位置:网站首页>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;
}
边栏推荐
- 2022 CCF中国开源大会会议通知(第三轮)
- 深入理解JVM-内存结构
- 建模该从哪一步开始?给你分析,给零基础的你一些学习建议
- Reveal how the five operational management level of hundreds of millions of easily flow system
- ERROR: You don‘t have the SNMP perl module installed.
- Matlab论文插图绘制模板第42期—气泡矩阵图(相关系数矩阵图)
- epoll + 线程池 + 前后置服务器分离
- 群辉查看硬盘存储占用的方式
- 那些年我写过的语言
- Line the last time the JVM FullGC make didn't sleep all night, collapse
猜你喜欢
梅科尔工作室-14天华为培训七
Benchmarking Lane-changing Decision-making for Deep Reinforcement Learning
云图说丨初识华为云微服务引擎CSE
CS免杀姿势
开源生态研究与实践| ChinaOSC
【leetcode】剑指 Offer II 008. 和大于等于 target 的最短子数组(滑动窗口,双指针)
【leetcode】剑指 Offer II 009. 乘积小于 K 的子数组(滑动窗口、双指针)
网络协议-TCP、UDP区别及TCP三次握手、四次挥手
微导纳米IPO过会:年营收4.28亿 君联与高瓴是股东
MySQL master-slave, 6 minutes you master!
随机推荐
glide set gif start stop
pytorch框架实现老照片修复功能详细演示(GPU版)
Interview Blitz: What Are Sticky Packs and Half Packs?How to deal with it?
标准C语言学习总结11
dpkg强制安装软件
Unity gets the actual coordinates of the ui on the screen under the canvas
那些年我写过的语言
【木马免杀】
【STM32】标准库-自定义BootLoader
【夜莺监控方案】08-监控msyql集群(prometheuse+n9e+mysqld_exporter)
Postgresql-xl全局快照与GTM代码走读(支线)
【leetcode】剑指 Offer II 009. 乘积小于 K 的子数组(滑动窗口、双指针)
告诉你0基础怎么学好游戏建模?
Network protocol-TCP, UDP difference and TCP three-way handshake, four wave
「游戏建模干货」建模大师几步操作,学习经典,赶紧脑补一下吧
ctfshow php特性
力扣刷题之合并两个有序数组
阿洛的反思
2022 CCF中国开源大会会议通知(第三轮)
花 30 美金请 AI 画家弄了个 logo,网友:画得非常好,下次别画了!