程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Sword finger offer python:54 balanced binary tree

編輯:Python

subject : Enter a binary tree , Determine whether the binary tree is a balanced binary tree .

Balanced binary trees : Balanced binary search tree , Also known as AVL Trees .

It is an empty tree or its left and right subtrees The absolute value of the height difference does not exceed 1, And both the left and right subtrees are a balanced binary tree .

recursive .

Code :

class Solution:
def __init__(self):
self.balance = True
def find(self , root):
self.func(root)
return self.balance
def func(self , root):
if not root:
return 0
left = self.func(root.left)
right = self.func(root.right)
if abs(left - right) > 1 and self.balance:
self.balance = False
return max(left , right) + 1


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved