關(guān)于二叉樹(shù)高度的計(jì)算抄瑟,通過(guò)遞歸的方式得到穗泵,跳出遞歸的條件是,當(dāng)結(jié)點(diǎn)是None的時(shí)候滑负,高度為0
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def get_height(self, root):
if root is None:
return 0
left_height = self.get_height(root.left)
right_height = self.get_height(root.right)
return max(left_height, right_height) + 1