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.
給一個二叉樹,計算二叉樹的直徑,直徑是任意兩個節點之間的最長路徑。
解法1: traverse every node to get max of leftDepth, then rightDepth, then add those 2 at every node, calculate the max depth by helper. Complexity: Time O(N^2) Space O(N^2)
解法2:find out the max of leftDepth & rightDepth while at each node, meanwhile update the total max. Complexity: Time O(N) Space O(N)
最長路徑有兩種情況:
1. 最長條路徑經過根節點,那么只需要找出根節點的左右兩棵子樹的最大深度然后相加即可。
2. 最長路徑沒有經過根節點,那么只需要找出根節點的左子樹或者根節點的右子樹作為根的最長路徑度即可。遞歸調用,自底向上查找子樹的深度,如果某一個左子樹與右子樹深度之和大於當前紀錄的直徑,那么替換為當前直徑,遞歸完成之后即可找出直徑。
Java:
public class Solution { int max = 0; public int diameterOfBinaryTree(TreeNode root) { if(root == null) return 0; max = Math.max(max, helper(root.left) + helper(root.right)); diameterOfBinaryTree(root.left); diameterOfBinaryTree(root.right); return max; } public int helper(TreeNode root){ if(root == null) return 0; return 1 + Math.max(helper(root.left), helper(root.right)); } }
Java:
public class Solution { int max = 0; public int diameterOfBinaryTree(TreeNode root) { maxDepth(root); return max; } public int maxDepth(TreeNode root){ if(root == null)return 0; int left = maxDepth(root.left); int right = maxDepth(root.right); max = Math.max(max, left + right); return 1 + Math.max(left, right); } }
Java:
class Solution { int depth = 0; public int diameterOfBinaryTree(TreeNode root) { search(root); return depth; } private int search(TreeNode root) { if (root == null) { return 0; } int left = search(root.left); int right = search(root.right); if (depth < left + right) { depth = left + right; } return left > right ? left + 1 : right + 1; } }
Python:
class Solution(): def diameter(self, root): if not root: return 0 res = self.depth(root.left) + self.depth(root.right) return max(res, max(self.diameter(root.left), self.diameter(root.right))) def depth(self, node): if not node: return 0 return 1 + max(self.depth(node.left), self.depth(node.right))
Python:
class Solution(): def __init__(self): self.max = 0 def diameter(self, root): self.helper(root) return self.max def helper(self, root): if not root: return 0 left = self.helper(root.left) right = self.helper(root.right) self.max = max(self.max, left + right) return 1 + max(left, right)
Python:
def height(node): if node is None: return 0 ; return 1 + max(height(node.left), height(node.right)) def diameter(root): if root is None: return 0; lheight = height(root.left) rheight = height(root.right) ldiameter = diameter(root.left) rdiameter = diameter(root.right) return max(lheight + rheight, max(ldiameter, rdiameter))
Python:
class Solution(object): def diameterOfBinaryTree(self, root): """ :type root: TreeNode :rtype: int """ def depth(root, diameter): if not root: return 0, diameter left, diameter = depth(root.left, diameter) right, diameter = depth(root.right, diameter) return 1 + max(left, right), max(diameter, 1 + left + right) return depth(root, 1)[1] - 1
C++: perfect solution , runtime = 26 ms
class Solution7 { public: int diameterOfBinaryTree(TreeNode *root) { if (!root) return 0; int res = depthOfNode(root->left) + depthOfNode(root->right); return max(res, max(diameterOfBinaryTree(root->left), diameterOfBinaryTree(root->right))); } int depthOfNode(TreeNode *node) { if (!node) return 0; return max(depthOfNode(node->left), depthOfNode(node->right)) + 1; } };
類似題目:
[LeetCode] 104. Maximum Depth of Binary Tree 二叉樹的最大深度
All LeetCode Questions List 題目匯總