[LC] 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as:
a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example 1:
Given the following tree [3,9,20,null,null,15,7]
:
- 3
- / \
- 9 20
- / \
- 15 7
Return true.
Example 2:
Given the following tree [1,2,2,3,3,null,null,4,4]
:
- 1
- / \
- 2 2
- / \
- 3 3
- / \
- 4 4
Return false.
- # Definition for a binary tree node.
- # class TreeNode:
- # def __init__(self, x):
- # self.val = x
- # self.left = None
- # self.right = None
- class Solution:
- def isBalanced(self, root: TreeNode) -> bool:
- res = self.helper(root)
- return False if res == -1 else True
- def helper(self, root):
- if root is None:
- return 0
- left = self.helper(root.left)
- right = self.helper(root.right)
- if left == -1 or right == -1 or abs(left - right) > 1:
- return -1
- return max(left, right) + 1
[LC] 110. Balanced Binary Tree的更多相关文章
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 110. Balanced Binary Tree - LeetCode
Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- [LeetCode] 110. Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Leetcode 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- [LeetCode]题解(python):110 Balanced Binary Tree
题目来源 https://leetcode.com/problems/balanced-binary-tree/ Given a binary tree, determine if it is hei ...
随机推荐
- js filter()用法小结
/* filter() 对数组中的每个元素都执行一次指定的函数(callback),并且创建一个新的数组, 该数组元素是所有回调函数执行时返回值为 true 的原数组元素.它只对数组中的 非空元素执行 ...
- 题解 P1317 【低洼地】
题目 这题挺简单的,没必要用数组 [分析] 需要判断的是低洼地的数量 通过对题目中图进行分析,显然可以发现低洼地的定义: 若数组中存在一个数值相同的连续区间,这个区间端点外相邻两点的数值都大于该区间的 ...
- git子模块使用
如下项目有多个标红的子模块 1.首先进入每个子模块目录,init初始化子模块仓库,然后提交远程. 2.在每个子目录都初始化好仓库后,进入lv-qggz主目录,只初始化该仓库,然后依次添加子模块的仓库地 ...
- 吴裕雄--天生自然 PHP开发学习:数组
<?php $cars=array("Volvo","BMW","Toyota"); echo "I like " ...
- hdu 2072(字典树模板,set,map均可做)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...
- nfs自动挂载
服务器端 /etc/exports /mnt *(rw,sync,no_root_squash,anonuid=500,anongid=500)systemctl restart nfs 客户端 挂载 ...
- linux下的hashpump安装
hashpump是linux上的一个进行hash长度拓展攻击的工具 安装: git clone https://github.com/bwall/HashPump apt-get install g+ ...
- Transmission添加SSL访问
0.准备工作 0.1.在App Center中安装Entware-ng 0.2.以admin用户登录SSH到NAS 0.3.申请SSL证书,可以找免费的申请一个 0.4.公网IP和域名,这个要和SSL ...
- vue多选验证
vue select 多选 验证 <FormItem :prop="'formList.'+index+'.name'" label="姓名" :rule ...
- Java 进制转换(二进制(负),八进制,十进制,十六进制),位运算、逻辑运算(2)
负数的二进制表现形式:其实就是该数的绝对值取反+1. 进制转换(二进制,八进制,十进制,十六进制),原理解析 十六进制的表现形式: (2)(与.异或.左移.右移.三元运算符)