class Solution {
//使用flag做剪枝,如果已有不平衡子树,则不继续往下递归
private boolean flag = true;
public boolean isBalanced(TreeNode root) {
height(root);
return flag;
}
public int height(TreeNode root) {
if (root == null || !flag) {
return 0;
}
int left = (height(root.left));
int right = (height(root.right));
if (Math.abs(left - right) > 1) {
flag = false;
}
return Math.max(left, right) + 1;
}
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isBalanced(root *TreeNode) bool {
if root == nil {
return true
}
return abs(height(root.Left) - height(root.Right)) <= 1 && isBalanced(root.Left) && isBalanced(root.Right)//子树也需要考虑平衡
}
func height(root *TreeNode) int{
if root == nil {
return 0
}
return max(height(root.Left), height(root.Right)) + 1
}
func max(a,b int) int {
if a > b {
return a
}
return b
}
func abs(a int) int {
if a < 0 {
return a * -1
}
return a
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isBalanced(root *TreeNode) bool {
if root == nil {
return true
}
return abs(maxDepth(root.Left), maxDepth(root.Right)) <= 1 && isBalanced(root.Left) && isBalanced(root.Right)
}
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
left := maxDepth(root.Left)
right := maxDepth(root.Right)
return max(left, right) + 1
}
func max(a , b int) int {
if a > b {
return a
}
return b
}
func abs(a, b int) int {
if (a - b) < 0 {
return b - a
}
return a - b
}
// C++实现
class Solution {
public:
bool isBalanced(TreeNode* root) {
if (root nullptr) return true;
if (maxDepth(root) -1) return false;
return true;
}
int maxDepth(TreeNode* root) {
if (root == nullptr) return 0;
int left = maxDepth(root->left);
if (left == -1) return -1;
int right = maxDepth(root->right);
if (right == -1) return -1;
if (abs(left - right) > 1) return -1;
return max(left, right) + 1;
}
class Solution {
public boolean isBalanced(TreeNode root) {
if(root==null){
return true;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.abs(right-left)<=1 && isBalanced(root.left) && isBalanced(root.right);
}
int maxDepth(TreeNode root){
if(root==null) return 0;
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left,right)+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) {
if(root == null) return true;
if(Math.abs(len(root.left)-len(root.right))>1) return false;
return isBalanced(root.left)&&isBalanced(root.right);
}
public int len(TreeNode node){
if(node==null) return 0;
int left = len(node.left);
int right = len(node.right);
return 1+Math.max(left,right);
}
}
六
/**
* 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) {
if (root null)
return true;
if (maxDepth(root) -1)
return false;
return true;
}
public int maxDepth(TreeNode root) {
if (root null)
return 0;
int left = maxDepth(root.left);
if (left -1) {
return -1;
}
int right = maxDepth(root.right);
if (right -1)
return -1;
if (Math.abs(left – right) > 1)
return -1;
return Math.max(left, right) + 1;
}
}
共以下 38 个回答
递归:首先判断左右子树的高度相差不超过1,然后再判断左右子树是否为平衡二叉树
高度函数也是基于递归
时间复杂度O(n2)
空间复杂度O(n)
递归比较每个结点左子树和右子树高度差
时间复杂度O(n*logN)
空间复杂度O(n)
maxDepth:后序遍历
idBalanced:先序遍历
通过
从叶子结点开始,判断是否是平衡的,逐渐往上判断直至根节点。 时间复杂度,对每个结点都有判断故为O(n) 空间复杂度,递归栈为O(n)
/**中文
* 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) {
if(root nullptr) return true;
int res = abs(Depth(root->right) – Depth(root -> left));
return res <= 1 && isBalanced(root->left) && isBalanced(root->right);
}
};
上一题的简单应用
自底向上递归
// C++实现
class Solution {
public:
bool isBalanced(TreeNode* root) {
if (root nullptr) return true;
if (maxDepth(root) -1) return false;
return true;
}
};
”’java
class Solution {
public boolean isBalanced(TreeNode root) {
if(root null){
return true;
}
if(Math.abs(maxDepth(root.left)-maxDepth(root.right)) <= 1){
return isBalanced(root.left) && isBalanced(root.right);
}
return false;
}
public int maxDepth(TreeNode root){
if(root null){
return 0;
}
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
return leftDepth > rightDepth ? leftDepth+1:rightDepth+1;
}
}
”’
时间复杂度O(n)
空间复杂度O(n)
时间复杂度:O(n)
空间复杂度:O(n)
代码
boolean jude =true;
public boolean isBalanced(TreeNode root) {
//左右子树最大深度不能超过1
isBalanced1(root);
return jude;
}
public int isBalanced1(TreeNode root) {
if(rootnull) return 0;
}
递归调用
时间复杂度O(nlogn)空间复杂度O(n)c++ 代码 递归调用
};
时间: O(n)
空间:O(n)
Day6:下面是代码
“`c++
//时间复杂度:O(n)
//空间复杂度:O(n)
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root==NULL) return true;
if(maxDepth(root)==-1) return false;
return true;
}
int maxDepth(TreeNode* root) {
if(root==NULL) return 0;
int l=maxDepth(root->left);
if(l==-1) return -1;
int r=maxDepth(root->right);
if(r==-1) return -1;
if(abs(l-r)>1) return -1;
return max(l,r)+1;
}
};
代码
时间复杂度:?
时间复杂度:O(n)
空间复杂度:O(n)
打卡
六
/**
* 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) {
if (root null)
return true;
if (maxDepth(root) -1)
return false;
return true;
}
public int maxDepth(TreeNode root) {
if (root null)
return 0;
int left = maxDepth(root.left);
if (left -1) {
return -1;
}
int right = maxDepth(root.right);
if (right -1)
return -1;
if (Math.abs(left – right) > 1)
return -1;
return Math.max(left, right) + 1;
}
}