Leetcode OJ contest24 543 Diameter of Binary Tree
来源:互联网 发布:淘宝店铺改名字怎么改 编辑:程序博客网 时间:2024/06/05 18:26
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxdiadepth=0;
int dfs(TreeNode* root){
if(root==NULL)
return 0;
int leftdepth=dfs(root->left);
int rightdepth=dfs(root->right);
if(leftdepth+rightdepth>maxdiadepth)
maxdiadepth=leftdepth+rightdepth;
return max(leftdepth+1,rightdepth+1);
}
int diameterOfBinaryTree(TreeNode* root) {
dfs(root);
return maxdiadepth;
}
};
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxdiadepth=0;
int dfs(TreeNode* root){
if(root==NULL)
return 0;
int leftdepth=dfs(root->left);
int rightdepth=dfs(root->right);
if(leftdepth+rightdepth>maxdiadepth)
maxdiadepth=leftdepth+rightdepth;
return max(leftdepth+1,rightdepth+1);
}
int diameterOfBinaryTree(TreeNode* root) {
dfs(root);
return maxdiadepth;
}
};
0 0
- Leetcode OJ contest24 543 Diameter of Binary Tree
- LeetCode OJ 543 Diameter of Binary Tree [Easy]
- leetcode(543). Diameter of Binary Tree
- LeetCode 543 Diameter of Binary Tree
- 【Leetcode-easy-543】Diameter of Binary Tree
- leetcode编程记录9 #543 Diameter of Binary Tree
- Leetcode-543. Diameter of Binary Tree
- [leetcode] 543. Diameter of Binary Tree
- 【Leetcode】543. Diameter of Binary Tree
- [LeetCode]543. Diameter of Binary Tree
- [LeetCode]543. Diameter of Binary Tree
- leetcode-543. Diameter of Binary Tree
- leetcode-504-Diameter of Binary Tree
- leetcode-504-Diameter of Binary Tree
- [leetcode]-543. Diameter of Binary Tree
- LeetCode 543. Diameter of Binary Tree
- (LeetCode) 543. Diameter of Binary Tree
- leetcode 543. Diameter of Binary Tree
- 2012年北邮计算机学院上机题目
- selenium WebDriver简单使用
- CSS选择器
- oop编程 探测器实例
- 【ZT】面试的65个技巧性回答
- Leetcode OJ contest24 543 Diameter of Binary Tree
- 数据结构(二叉树)
- Ubuntu安装genymotion
- 字符串长度及易错
- 学习运维——网络文件协议-CIFS与NFS
- 蓝桥杯-转方阵
- 当你从浏览器地址栏中输入URL回车后发生了什么
- 各大浏览器内核特性及对应的Browserhacks举例
- Cookie, LocalStorage 与 SessionStorage