# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
#
# @param root TreeNode类
# @return int整型
#
class Solution:
def maxDepth(self , root ):
# write code here
#idea: bottom-up, post order
# input: node
# output: max depth from current node
# 1. base case: if node == none, return max_depth = 0
# 2. go to left, right subtree to obtain max depth from left, right
# 3. return max depth of left/right +1, including current parent node
#
if not root:
return 0
l_max = self.maxDepth(root.left)
r_max = self.maxDepth(root.right)
return max(l_max, r_max) + 1
Iteration DFS method
class Solution:
def maxDepth(self , root ):
#
# DFS iteration 写法
# iteration method: use array to simulate stack
# recursion 传进来的参数都 写到stack的tuple里面
#
#
if not root:
return 0
depth = 0
stack = [(root,0, 1 )]
max_depth = 0
#l_max = 0
#r_max = 0
while stack:
# input to child node in one level = parent node, count of parent
# other input parameter: depth
node, cnt,depth = stack.pop(-1)
if cnt == 0:
#preorder
stack.append((node, cnt+1, depth))
# always go to left child first, in DFS
if node.left:
# input to next level
stack.append((node.left, 0,depth+1))
if cnt ==1:
#in-order
# when cnt =1, then we can take the result from left node
stack.append((node, cnt+1, depth))
if node.right:
# input to next level
stack.append((node.right, 0, depth+1))
if cnt ==2:
# post-order back , exit tree
# global max method
max_depth = max(max_depth,depth)
return max_depth
Tree 的iteration general的方法/DFS + 每一层都有返回的写法
在原来的 iteration method for Tree traversal 的方法上添加以下两条
对于每一层的node的输入,可以在 stack append (node.left, cnt, other inputs)的tuple里面给 child node进行input的添加
class Solution:
def maxDepth(self , root ):
#
# DFS iteration 写法
# iteration method: use array to simulate stack
# recursion 传进来的参数都 写到stack的tuple里面
#
#
if not root:
return 0
depth = 0
stack = [(root,0, 1 )]
max_depth = 0
# result stack
res_stack =[]
#l_max = 0
#r_max = 0
while stack:
# input to child node in one level = parent node, count of parent
# other input parameter: depth
node, cnt,depth = stack.pop(-1)
if cnt == 0:
#preorder
stack.append((node, cnt+1, depth))
# always go to left child first, in DFS
if node.left:
# input to next level, the third value in tuple
# is input to the left child
stack.append((node.left, 0,depth))
if cnt ==1:
#in-order
# when cnt =1, then we can take the result from left node
stack.append((node, cnt+1, depth))
if node.right:
# input to next level
stack.append((node.right, 0, depth))
if cnt ==2:
# post-order back , exit tree node
# Return result to stack
# default value =0
r_res = 0
l_res = 0
if res_stack and node.right:
r_res = res_stack.pop(-1)
if res_stack and node.left:
l_res = res_stack.pop(-1)
# append result of this node to result stack
depth = max(l_res, r_res) +1
res_stack.append(depth)
# global max method
#max_depth = max(max_depth,depth)
return res_stack[-1]
Iteration BFS method/ Level order method
class Solution:
def maxDepth(self , root ):
#
# BFS iteration 写法
# iteration method: use array to simulate queue
# 每一层的level都+= 1
#
#
if not root:
return 0
queue = [root] # store node in current level
level = [] # store the node in the next level
depth = 0
while queue:
node = queue.pop(0)
if node.left:
level.append(node.left)
if node.right:
level.append(node.right)
if not queue:
depth += 1
queue = level
level = []
return depth