实现一个函数,检查二叉树是否平衡。在这个问题中,平衡树的定义如下:任意一个节点,其两棵子树的高度差不超过 1。
示例 1:
给定二叉树 [3,9,20,null,null,15,7]示例 2:
3
/ \
9 20
/ \
15 7
返回 true 。
给定二叉树 [1,2,2,3,3,null,null,4,4]
1
/ \
2 2
/ \
3 3
/ \
4 4
返回 false 。
我们设计一个函数
函数
- 如果
$root$ 为空,则返回$0$ ; - 否则,我们递归调用
$dfs(root.left)$ 和$dfs(root.right)$ ,并判断$dfs(root.left)$ 和$dfs(root.right)$ 的返回值是否为$-1$ ,如果不为$-1$ ,则判断$abs(dfs(root.left) - dfs(root.right)) <= 1$ 是否成立,如果成立,则返回$max(dfs(root.left), dfs(root.right)) + 1$ ,否则返回$-1$ 。
在主函数中,我们只需要调用 true
,否则返回 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:
def dfs(root: TreeNode):
if root is None:
return 0, True
a, b = dfs(root.left)
c, d = dfs(root.right)
return max(a, c) + 1, abs(a - c) <= 1 and b and d
return dfs(root)[1]
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
return dfs(root) >= 0;
}
private int dfs(TreeNode root) {
if (root == null) {
return 0;
}
int l = dfs(root.left);
int r = dfs(root.right);
if (l < 0 || r < 0 || Math.abs(l - r) > 1) {
return -1;
}
return Math.max(l, r) + 1;
}
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
function<int(TreeNode*)> dfs = [&](TreeNode* root) {
if (!root) {
return 0;
}
int l = dfs(root->left);
int r = dfs(root->right);
if (l == -1 || r == -1 || abs(l - r) > 1) {
return -1;
}
return max(l, r) + 1;
};
return dfs(root) >= 0;
}
};
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isBalanced(root *TreeNode) bool {
var dfs func(*TreeNode) int
dfs = func(root *TreeNode) int {
if root == nil {
return 0
}
l, r := dfs(root.Left), dfs(root.Right)
if l == -1 || r == -1 || abs(l-r) > 1 {
return -1
}
return max(l, r) + 1
}
return dfs(root) >= 0
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function isBalanced(root: TreeNode | null): boolean {
const dfs = (root: TreeNode | null): number => {
if (!root) {
return 0;
}
const l = dfs(root.left);
const r = dfs(root.right);
if (l === -1 || r === -1 || Math.abs(l - r) > 1) {
return -1;
}
return Math.max(l, r) + 1;
};
return dfs(root) >= 0;
}
# 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:
def dfs(root: TreeNode):
if root is None:
return 0
l, r = dfs(root.left), dfs(root.right)
if l == -1 or r == -1 or abs(l - r) > 1:
return -1
return max(l, r) + 1
return dfs(root) >= 0