543. Diameter of Binary Tree

# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def __init__(self): self.ans = 1 def diameterOfBinaryTree(self, root): """ :type root: TreeNode :rtype: int """ if root == None: return 0 else: L = self.maxDepth(root.left) R = self.maxDepth(root.right) def maxDepth(self, node): """ Input a node. Use DFS to get the max depth starting from that node. This is used for calculating diameters later. """ if node == None: return 0 else: return max(self.maxDepth(node.left), self.maxDepth(node.right)) + 1

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.