【LeetCode】543. Diameter of Binary Tree
来源:互联网 发布:筹备淘宝静物工作室 编辑:程序博客网 时间:2024/05/22 04:26
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
Example:
Given a binary tree
1 / \ 2 3 / \ 4 5
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
Note:The length of path between two nodes is represented by the number of edges between them.
题目是求二叉树的任意两个结点之间的最长路径。
后根遍历即可。
class Solution {public: int maxx=0; int DFS(TreeNode* root){ if(!root) return 0; int left=DFS(root->left); int right=DFS(root->right); maxx=max(left+right,maxx); return max(left,right)+1; } int diameterOfBinaryTree(TreeNode* root) { DFS(root); return maxx; }};
阅读全文
0 0
- 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]-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: Python]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 (Java)
- Leetcode-543. Diameter of Binary Tree
- 《并发编程》--11.允许多个线程同时访问的信号量Semaphpre
- PAT (Basic Level) Practise (中文) 1004成绩排名(20)
- SpringMVC的REST风格的四种请求方式
- C# 系统应用之TreeView控件显示树状磁盘文件目录及加载图标
- chkconfig用法及Linux的运行级别
- 【LeetCode】543. Diameter of Binary Tree
- 机器学习之正则化(Regularization)
- 【博览网】C++标准库——第二周课程笔记
- Android:SmartTabLayout使用
- Python计算谷歌身份验证器(google authenticator)的验证码
- 第四篇 HTML 表单深入了解、注释和a标签的运用
- Python 环境搭建
- php 压缩zip包压缩
- 【Python main 函数以及 __name__属性】