A simple Python solution


  • 0
    L
    class Solution(object):
        def diameterOfBinaryTree(self, root):
            self.s = 0
            self.dfs(root)
            return self.s
            
        def dfs(self, root):
            if not root: return 0
            l = self.dfs(root.left)
            r = self.dfs(root.right)
            self.s = max(self.s, l + r)
            return max(l, r) + 1
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.