题解 | #二叉树的深度#
二叉树的深度
http://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b
假如 你现在已经求出了 左 右 子树的高度,现在你该怎么做呢?
1.比较 左右子树的高度 取大值
2.加上当前高度(+1) 返给 上层
# -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def TreeDepth(self, pRoot): # write code here if pRoot==None: return 0 l=self.TreeDepth(pRoot.left) r=self.TreeDepth(pRoot.right) #h为子树的高度 h=(l>r and l or r) return h+1