diff --git a/Python/543_Diameter_Of_Binary_Tree.py b/Python/543_Diameter_Of_Binary_Tree.py new file mode 100644 index 0000000..2693354 --- /dev/null +++ b/Python/543_Diameter_Of_Binary_Tree.py @@ -0,0 +1,27 @@ +''' +Given the root of a binary tree, return 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. +The length of a path between two nodes is represented by the number of edges between them. +''' +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def diameterOfBinaryTree(self, root: TreeNode) -> int: + def diameterTreeUtil(root): + if root == None: return [0,0] + + leftTree = diameterTreeUtil(root.left) + rightTree = diameterTreeUtil(root.right) + + internalPath = max(leftTree[1],rightTree[1]) + if leftTree[0] + rightTree[0] + 1 > internalPath: internalPath = leftTree[0] + rightTree[0] + 1 + + return [max(leftTree[0],rightTree[0])+1,internalPath] + + if root == None: return 0 + result = diameterTreeUtil(root) + return max(result)-1 \ No newline at end of file