Leetcode:minimum Depth of Binary Tree "Python Edition"

Source: Internet
Author: User

1, the recursive call in the class to add self;

2, Root is None, return 0

3, Root is not about none,root child for none, return 1

4. Return the L and r minimum depths, and L and R are initially maximum values;

1 #Definition for a binary tree node2 #class TreeNode:3 #def __init__ (self, x):4 #self.val = x5 #self.left = None6 #self.right = None7 8 classSolution:9     #@param root, a tree nodeTen     #@return An integer One     defmindepth (self, root): A         ifRoot = =None: -             return0 -         ifRoot.left==none androot.right==None: the             return1 -L,r = 9999,9999 -         ifroot.left!=None: -L =self.mindepth (root.left) +         ifroot.right!=None: -R =self.mindepth (root.right) +         ifl<r: A             return1 +L at         return1+r

Leetcode:minimum Depth of Binary Tree "Python Edition"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.