当前位置:网站首页>Leetcode-543- diameter of binary tree
Leetcode-543- diameter of binary tree
2022-06-21 23:31:00 【z754916067】
subject

Ideas
- I don't think this will be done recursively … Sure enough, I haven't written the question for a long time , I have no idea about simple questions .
- Find out the diameter and length of each node as the root node, and then find the maximum value ? That's exactly the idea , Passed .
Code
int ans=Integer.MIN_VALUE;
public int diameterOfBinaryTree(TreeNode root) {
if(root.left==null && root.right==null) return 0;
// meaningless Placeholder
int flag=DFS(root);
return ans;
}
public int DFS(TreeNode root){
// Take the current node as the root node If you go to the leaf node Note the diameter of the current node as the root node is 0 But take it as a side Then return to 1
if(root.left==null && root.right==null) return 1;
// Otherwise, calculate
int leftval=0,rightval=0;
if(root.left!=null) leftval = DFS(root.left);
if(root.right!=null) rightval = DFS(root.right);
if(leftval+rightval>ans) {
ans=leftval+rightval;
}
// Similarly, return the maximum value between two sides +1
return Math.max(leftval,rightval)+1;
}
边栏推荐
- 项目变更管理
- Specific methods of using cloud development to realize wechat payment
- 软件测试概念篇
- Record the abnormal task status caused by an MQ concurrent consumption
- 關於 麒麟系統開發錯誤“fatal error: GL/gl.h: No such file or directory“ 的解决方法
- Personal stock trading experience
- uniapp在解决谷歌浏览器跨域问题,在谷歌浏览器运行
- [highly recommended] markdown grammar
- 【用四道刁钻例题带你理解】数据在内存中存储的方式
- Common forms in QT
猜你喜欢

redis主从复制(九)

danfoss丹佛斯变频器维修VLT5000/VLT6000/VLT8000

What are the trends of cloud computing in 2022?

H5之微信授权登陆 (uniapp网页版微信授权登录)

Elementary transformation of numpy matrix

WSL 2 的安装过程(以及介绍)

Uniapp solves the cross domain problem of Google browser and runs in Google browser

树莓派开发笔记(十五):树莓派4B+从源码编译安装mysql数据库

4. ESP8266通过OLED实时显示DHT11温湿度参数

Some users of uniapp wechat authorization cannot be authorized normally
随机推荐
If you spend 200W to buy traffic, it is better to start at 0 cost and make an independent station with high private domain operation income!
Uniapp play video, download video to mobile photo album, add download progress bar function (step on pit record)
CISSP certification 2021 textbook OSG 9th Edition added (modified) knowledge points: comparison with the 8th Edition
WSL 2 的安装过程(以及介绍)
软件测试 答疑篇
[understanding pointer] advanced level of pointer
樹莓派開發筆記(十六):樹莓派4B+安裝mariadb數據庫(mysql開源分支)並測試基本操作
Using JS function in wxml file of applet
在小程序的 wxml 文件中使用 js 函数
阿里出品!图形化的ant脚本——IDEA插件CloudToolkit
Detailed explanation of C language [implicit type conversion] and [explicit type conversion]
解决笔记本电脑(i)某个键的字母按不出来
Sigir2022 | modélisation des préférences des utilisateurs dans le système de recommandation dialogique
leetcode1337. 矩阵中战斗力最弱的K行
你有一个机会,这里有一个舞台
Set -set
RK3568开发笔记(二):入手RK3568开发板的套件介绍、底板介绍和外设测试
Uniapp encapsulates the request function to achieve unique login. One account can only log in to one device at the same time
关于 国产麒麟Qt编译报错“xxx.pri has modification time xxxx s in the futrue“ 的解决方法
About the designer of qtcreator the solution to the problem that qtdesigner can't pull and hold controls normally