LeetCode 543. Diameter of Binary Tree
2018-02-02 03:39
387 查看
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.
题目大意:给一个二叉树,计算出任意两个节点中最长的长度并返回结果~
分析:计算每个节点的深度,并在dfs过程中将每个节点左边深度+右边深度的值的最大的值保存在ans中返回~
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.
题目大意:给一个二叉树,计算出任意两个节点中最长的长度并返回结果~
分析:计算每个节点的深度,并在dfs过程中将每个节点左边深度+右边深度的值的最大的值保存在ans中返回~
class Solution { public: int diameterOfBinaryTree(TreeNode* root) { dfs(root); return ans; } private: int ans = 0; int dfs(TreeNode* root) { if (root == NULL) return 0; int l = dfs(root->left), r = dfs(root->right); ans = max(ans, l + r); return max(l, r) + 1; } };
相关文章推荐
- LeetCode 111. Minimum Depth of Binary Tree
- 【LeetCode】104.Maximum Depth of Binary Tree
- LeetCode-111. Minimum Depth of Binary Tree
- 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)
- LeetCode: 104. Maximum Depth of Binary Tree
- 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)
- leetcode 543. Diameter of Binary Tree
- [勇者闯LeetCode] 104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- Leetcode 111. Minimum Depth of Binary Tree
- leetcode记录 104. Maximum Depth of Binary Tree
- LeetCode *** 111. Minimum Depth of Binary Tree
- Leetcode 111. Minimum Depth of Binary Tree
- leetcode-111. Minimum Depth of Binary Tree
- LeetCode-226. Invert Binary Tree
- LeetCode: Binary Tree Sum相关题目合集
- 渣渣写LEETCODE——104. Maximum Depth of Binary Tree
- leetcode 111. Minimum Depth of Binary Tree
- Leetcode 104. Maximum Depth of Binary Tree
- [Leetcode] 111. Minimum Depth of Binary Tree