leetcode104 python binary tree maximum depth

Given a binary tree, find its maximum depth.

The depth of a binary tree is the number of nodes on the longest path from the root node to the farthest leaf node.

Explanation: A  leaf node refers to a node without child nodes.

Example:
Given a binary tree  [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

Returns its maximum depth of 3 .


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:#recursive boundary
            return 0
        else:
            l=1+self.maxDepth(root.left)#recursion
            r=1+self.maxDepth(root.right)
            return max(l,r)
        



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325694630&siteId=291194637