![]()
#############这道题的解题思路:同从32题上到下打印二叉树,34题二叉树中和为某一值的路径
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
import copy
class Solution:
def TreeDepth(self, pRoot):
# write code here
# 先判断边界条件
if pRoot == None:
return 0
# 采用广度优先遍历
# 构造一个辅助list
support = [pRoot]
# 再构造一个存访list列表的list
supportarray = [[pRoot.val]]
retarray = []
maxdepth = 0
while support:
temp = support[0]
templist = supportarray[0]
if temp.left==None and temp.right==None:
templen=len(templist)
if templen>maxdepth:
maxdepth = templen
if temp.left:
support.append(temp.left)
newtemplist = copy.copy(templist)
newtemplist.append(temp.left.val)
supportarray.append(newtemplist)
if temp.right:
support.append(temp.right)
newtemplist = copy.copy(templist)
newtemplist.append(temp.right.val)
supportarray.append(newtemplist)
del supportarray[0]
del support[0]
return maxdepth