关于二叉树高度的计算,通过递归的方式得到,跳出递归的条件是,当结点是None的时候,高度为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
网友评论