Sunday, September 7, 2014

Leetcode: Maximum Depth of Binary Tree @Python

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.
# Definition for a  binary tree node
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    # @param root, a tree node
    # @return an integer
    def maxDepth(self, root):
        if(not root):
            return 0
        lDep=self.maxDepth(root.left)
        rDep=self.maxDepth(root.right)
        lrMaxDep=lDep if lDep>rDep else rDep
        return lrMaxDep+1

No comments :

Post a Comment