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