码迷,mamicode.com
首页 > 其他好文 > 详细

面试题55:二叉树的深度

时间:2019-08-16 22:44:38      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:temp   sel   打印二叉树   val   二叉树   解题思路   return   边界条件   http   

技术图片

#############这道题的解题思路:同从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

  

面试题55:二叉树的深度

标签:temp   sel   打印二叉树   val   二叉树   解题思路   return   边界条件   http   

原文地址:https://www.cnblogs.com/ivyharding/p/11366568.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!