Home > AI > Data Structure > Tree > Binary Search Tree >

leetcode-543-Diameter of Binary Tree

Solution: Python

class Solution(object):
    def diameterOfBinaryTree(self, root):
        
        self.ans = 0
        def path(root):
            if not root: return 0
            
            L = path(root.left)
            R = path(root.right)
            
            self.ans = max(self.ans, L+R)
            return max(L, R)+1
        
        path(root)
        
        return self.ans

Leave a Reply