Question
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Hide Tags Tree Depth-first Search
Analysis
use depth-first search
Solution
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# @param {TreeNode} root
# @return {integer}
def maxDepth(self, root):
return self.search(root)
def search(self,subroot):
if subroot==None:
return 0
return 1+max(self.search(subroot.left), self.search(subroot.right))