Skip to content

Instantly share code, notes, and snippets.

@vamsitallapudi
Created April 12, 2020 06:19
Show Gist options
  • Save vamsitallapudi/6213c6eb886f1786847a0b683a90721b to your computer and use it in GitHub Desktop.
Save vamsitallapudi/6213c6eb886f1786847a0b683a90721b to your computer and use it in GitHub Desktop.
class Solution:
def diameterOfBinaryTree(self, root: TreeNode) -> int:
self.globalLongestPath = 0
self.depth(root)
return self.globalLongestPath
def depth(self, node:TreeNode)-> int:
if not node:
return 0
left = self.depth(node.left)
right = self.depth(node.right)
self.globalLongestPath = max(self.globalLongestPath, left + right)
return max(left,right)+1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment