Leetcode Brush title Record [python]--104 Maximum Depth of Binary Tree

Source: Internet
Author: User

First, preface

For this kind of abstract problem there is self usage, but also more practice, this question is to refer to other answers to do.

Second, title 104 Maximum Depth of binary Tree

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along, the longest path from the root node to the farthest leaf node.

#Definition for a binary tree node.#class TreeNode (object):#def __init__ (self, x):#self.val = x#self.left = None#self.right = Noneclasssolution (object):defmaxDepth (self, root):""": Type Root:TreeNode:rtype:int"""        ifroot==None:return0returnMax (Self.maxdepth (Root.left), self.maxdepth (root.right)) +1

Idea: Traversing the left and right subtree of the root node (P.S. Root node is relative, not just the initial root node), traverse one layer, add one, compare the left and right result size, the final result is added one.

Do not know the right to say, such as wrong, later understand more thoroughly corrected.

Leetcode Brush title Record [python]--104 Maximum Depth of Binary Tree

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.