给定一个二叉树,判断它是否是高度平衡的二叉树。

本题中,一棵高度平衡二叉树定义为:

一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。

例:

输入:root = [3,9,20,null,null,15,7]
输出:true

解析:

写一个深度函数,计算左子树和右子树的深度,然后进行比对,对子树也进行判断。

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):def depth(self, root):  # 计算深度函数if root is None:return 0else:return 1 + max(self.depth(root.left), self.depth(root.right))def isBalanced(self, root):""":type root: TreeNode:rtype: bool"""if root is None:return Trueif abs(self.depth(root.left) - self.depth(root.right)) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right):  # 判断是否为平衡树return Trueelse:return False

平衡二叉树Python解法相关推荐

  1. LeetCode 111. Minimum Depth of Binary Tree--Java, Python解法--二叉树最小高度--迭代,递归

    题目地址:Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimu ...

  2. LeetCode 226. Invert Binary Tree--反转二叉树--C++,Python解法--递归,迭代做法

    题目地址:Invert Binary Tree - LeetCode Invert a binary tree. Example: Input: 4/ \2 7/ \ / \ 1 3 6 9 Outp ...

  3. LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题

    题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...

  4. LeetCode 458. Poor Pigs--智力题「小白鼠试毒」--C++,Python解法

    题目地址:Poor Pigs - LeetCode There are 1000 buckets, one and only one of them is poisonous, while the r ...

  5. LeetCode 230. Kth Smallest Element in a BST--C++,Python解法--面试真题--找二叉树中第K小的元素

    题目地址:Kth Smallest Element in a BST - LeetCode Given a binary search tree, write a function kthSmalle ...

  6. LeetCode 221. Maximal Square----动态规划--谷歌面试算法题--Python解法

    题目地址:Maximal Square - LeetCode Given a 2D binary matrix filled with 0's and 1's, find the largest sq ...

  7. LeetCode 148. Sort List--面试算法题--C++,Python解法

    LeetCode 148. Sort List–面试算法题–C++,Python解法 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大 ...

  8. LeetCode 20. Valid Parentheses--笔试题--Python解法

    题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...

  9. LeetCode 145. Binary Tree Postorder Traversal--后序遍历--先序遍历反向输出--递归,迭代--C++,Python解法

    题目地址:Binary Tree Postorder Traversal - LeetCode Given a binary tree, return the postorder traversal ...

最新文章

  1. 物体6-Dof pose estimation主流方法汇总
  2. 面对复杂业务,if-else coder 如何升级?
  3. Python画图(直方图、多张子图、二维图形、三维图形以及图中图)
  4. Spring框架最终注解标签注入方法
  5. tomcat启动后连接数据库连接慢的问题
  6. java中求1 2 =3,1. java dom编程艺术---杨涛、王建桥 -第3章 DOM(自总)
  7. 研磨设计模式之《观察者模式observer》
  8. win11开机数字小键盘失灵不能使用问题
  9. D - Miracle Tree(思维+构造+树的直径)
  10. vs2013右键菜单 0xC0000005: 读取位置 0x00000004 时发生访问冲突
  11. Vue.js下载与安装
  12. c语言atm程序个人总结,自己写的ATM模拟程序
  13. 【移动端聊天功能模板】Vue实现H5聊天系统,实现上下固定中间滚动布局,微信授权功能,自动滚动到底部【详细注释,一看就会】
  14. rabbitMq实现延迟队列
  15. Android display
  16. 深圳市华宇讯科技有限公司(卖座网)打靶归来
  17. PS美工教程:制作真实水面倒影效果详细步骤
  18. Java编程题笔试常用API
  19. matlab qam 格雷编码,89. 格雷编码
  20. lxl 大厅协议 -- [libcef部分]

热门文章

  1. 哪些类型的 Component 不需要参加 SAP Spartacus 的服务器端渲染?
  2. SAP Spartacus B2B 页面 Disable Confirmation 对话框的显示原理
  3. 用代码查看SAP Spartacus购物车内的行项目
  4. SAP Data Hub里的Constant Generator
  5. SAP CRM的订单模型移植到S4HANA后,在订单保存功能上作出的改进
  6. 推荐一款可以监控电脑软硬件和操作电脑的微信小程序,名叫HiPC
  7. SAP Fiori Elements遇到问题时,该如何调试?
  8. Fiori offline support : overrideRefreshHandling and injectRefreshList
  9. Angular dependency injection - how injection is parsed
  10. Equipment download and upload: all possible scenario