平衡二叉树Python解法
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 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解法相关推荐
- 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 ...
- 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 ...
- LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题
题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...
- LeetCode 458. Poor Pigs--智力题「小白鼠试毒」--C++,Python解法
题目地址:Poor Pigs - LeetCode There are 1000 buckets, one and only one of them is poisonous, while the r ...
- 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 ...
- LeetCode 221. Maximal Square----动态规划--谷歌面试算法题--Python解法
题目地址:Maximal Square - LeetCode Given a 2D binary matrix filled with 0's and 1's, find the largest sq ...
- LeetCode 148. Sort List--面试算法题--C++,Python解法
LeetCode 148. Sort List–面试算法题–C++,Python解法 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大 ...
- LeetCode 20. Valid Parentheses--笔试题--Python解法
题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...
- LeetCode 145. Binary Tree Postorder Traversal--后序遍历--先序遍历反向输出--递归,迭代--C++,Python解法
题目地址:Binary Tree Postorder Traversal - LeetCode Given a binary tree, return the postorder traversal ...
最新文章
- 物体6-Dof pose estimation主流方法汇总
- 面对复杂业务,if-else coder 如何升级?
- Python画图(直方图、多张子图、二维图形、三维图形以及图中图)
- Spring框架最终注解标签注入方法
- tomcat启动后连接数据库连接慢的问题
- java中求1 2 =3,1. java dom编程艺术---杨涛、王建桥 -第3章 DOM(自总)
- 研磨设计模式之《观察者模式observer》
- win11开机数字小键盘失灵不能使用问题
- D - Miracle Tree(思维+构造+树的直径)
- vs2013右键菜单 0xC0000005: 读取位置 0x00000004 时发生访问冲突
- Vue.js下载与安装
- c语言atm程序个人总结,自己写的ATM模拟程序
- 【移动端聊天功能模板】Vue实现H5聊天系统,实现上下固定中间滚动布局,微信授权功能,自动滚动到底部【详细注释,一看就会】
- rabbitMq实现延迟队列
- Android display
- 深圳市华宇讯科技有限公司(卖座网)打靶归来
- PS美工教程:制作真实水面倒影效果详细步骤
- Java编程题笔试常用API
- matlab qam 格雷编码,89. 格雷编码
- lxl 大厅协议 -- [libcef部分]
热门文章
- 哪些类型的 Component 不需要参加 SAP Spartacus 的服务器端渲染?
- SAP Spartacus B2B 页面 Disable Confirmation 对话框的显示原理
- 用代码查看SAP Spartacus购物车内的行项目
- SAP Data Hub里的Constant Generator
- SAP CRM的订单模型移植到S4HANA后,在订单保存功能上作出的改进
- 推荐一款可以监控电脑软硬件和操作电脑的微信小程序,名叫HiPC
- SAP Fiori Elements遇到问题时,该如何调试?
- Fiori offline support : overrideRefreshHandling and injectRefreshList
- Angular dependency injection - how injection is parsed
- Equipment download and upload: all possible scenario