题目:输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
思路:利用递归的方法,比较每个节点左子树和右子树子节点的大小,返回较大的值
时间复杂度o(n), 空间复杂度o(logn)
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def TreeDepth(self, pRoot):
if pRoot is None:
return 0
else:
return max(self.TreeDepth(pRoot.left),self.TreeDepth(pRoot.right))+1
非递归算法:
利用栈及标志位:
def TreeDepth2(self, pRoot):
if not pRoot:
return 0
depth = 0
stack, tag = [], []
pNode = pRoot
while pNode or stack:
while pNode:
stack.append(pNode)
tag.append(0)
pNode = pNode.left
if tag[-1] == 1:
depth = max(depth, len(stack))
stack.pop()
tag.pop()
pNode = None
else:
pNode = stack[-1]
pNode = pNode.right
tag.pop()
tag.append(1)
return depth