To prove safety offer_ balanced binary tree binary tree _

Balanced binary tree binary tree _

Description Title
input binary tree, the binary tree is determined whether a balanced binary tree.
Answers

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def IsBalanced_Solution(self, pRoot):
        # write code here
        if not pRoot:
            return True
        left_depth = self.Treedepth(pRoot.left)
        right_depth = self.Treedepth(pRoot.right)
        if abs(left_depth - right_depth)>1:
            return False
        return self.IsBalanced_Solution(pRoot.left) and self.IsBalanced_Solution(pRoot.right)
              
    def Treedepth(self, pRoot):
        if not pRoot:
            return 0
        left = self.Treedepth(pRoot.left) + 1
        right = self.Treedepth(pRoot.right) + 1
        return max(left,right)
Published 31 original articles · won praise 0 · Views 718

Guess you like

Origin blog.csdn.net/freedomUSTB/article/details/105156732