LeetCode——平衡二叉树
题目描述:
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:true
示例 2:
输入:root = [1,2,2,3,3,null,null,4,4]
输出:false
示例 3:
输入:root = []
输出:true
提示:
树中的节点数在范围 [0, 5000] 内
-104 <= Node.val <= 104
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/balanced-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
解法一:我们使用自顶向下的方法递归判断,是否是平衡二叉树。判断左右子树的高度之差是否小于等于1,再分别判断根节点的左右子树是否是平衡二叉树,满足两个条件时该树就为平衡二叉树,否则返回false。
解法二:方法一在从上往下递归时会调用多次height函数计算高度,使得时间复杂度较高,我们使用自底向上的方法判断是否是平衡二叉树,对于每个节点,我们只调用一次height函数。
我们先递归判断根节点的左右子树是否平衡,再判断根节点是否平衡。如果一个一棵子树是平衡的则返回其高度,如果不是平衡的则返回-1。如果存在一棵子树不是平衡的,那这整棵树都不是平衡的。
以下提供解法一和解法二的Java和Python代码。
解法一Java代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null)
return true;
if ((Math.abs(depth(root.left) - depth(root.right)) <= 1) && isBalanced(root.left) && isBalanced(root.right))
return true;
return false;
}
public int depth(TreeNode root){
if (root == null)
return 0;
return Math.max(depth(root.left), depth(root.right)) + 1;
}
}
LeetCode测试结果:
解法二Java代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root){
return height(root) >= 0;
}
public int height(TreeNode root){
if (root == null)
return 0;
int rightHeight = height(root.right);
int leftHeight = height(root.left);
if (rightHeight == -1 || leftHeight == -1 || (Math.abs(rightHeight - leftHeight) > 1))
return -1;
return Math.max(rightHeight, leftHeight) + 1;
}
}
LeetCode测试结果:
解法一Python代码:
# 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 isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if root is None:
return True
if (abs(self.height(root.left) - self.height(root.right)) <= 1) and self.isBalanced(root.left) and self.isBalanced(root.right):
return True
return False
def height(self, root):
if root is None:
return 0
return max(self.height(root.left), self.height(root.right)) + 1
LeetCode测试结果:
解法二Python代码:
# 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 isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
return self.height(root) >= 0
def height(self, root):
if root is None:
return 0
leftHeight = self.height(root.left)
rightHeight = self.height(root.right)
if abs(leftHeight - rightHeight) > 1 or leftHeight == -1 or rightHeight == -1:
return -1
return max(leftHeight, rightHeight) + 1
LeetCode测试结果: