110.Balanced Binary Tree从8ms到2ms到1ms

本文介绍了在LeetCode上解决判断二叉树是否平衡的问题,从初始8ms的解决方案逐步优化至1ms的过程。通过递归计算二叉树深度,并在过程中判断平衡性,最终实现高效算法。

在leetcode上面刷了道题,最开始用时8ms,改进后用时2ms,再改用时1ms,下面记录一下:


题目:

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.

简单地说,对于给定的一棵二叉树,判断它是否是平衡二叉树

8ms解法:
  • 容易想见,如果一棵树是平衡二叉树,那么它的左右子树高度差不超过1
  • 而且,如果一棵树是平衡二叉树,那么它的任一子树也是平衡二叉树

    于是,可以这样做:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        return isBalanced(root.left) && isBalanced(root.right) && depth(root.left) - depth(root.right) >= -1 && depth(root.left) - depth(root.right) <= 1;
    }
    private int depth(TreeNode root) {
        // TODO Auto-generated method stub
        if (root == null) {
            return 0;
        }
        return 1 + (depth(root.left) > depth(root.right) ? depth(root.left) : depth(root.right));
    }
}
2ms解法
  • 在上面的解法中,我们发现了一个问题,每次判断一个子树是否是平衡二叉树的时候,我们都要对它的左右子树求高度,也就是说,如果一个结点在这棵二叉树的k棵子树中出现过,那么要计算以这个结点为根节点的子树的高度k次,显然大大增加了时间开销。
  • 于是我利用了树结点中的value去记录以该结点为根节点的子树的高度,那么,当求一棵子树的高度时,可以先求根结点的左右子树的高度,之后将1+max(左子树高度,右子树高度)作为这棵子树的高度。

代码如下:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        root = getDepth(root);
        return isBalanceCore(root);
    }
    private boolean isBalanceCore(TreeNode root) {
        // TODO Auto-generated method stub
        if (root == null) {
            return true;
        }
        if (root.left == null && root.right == null) {
            return true;
        }
        if (root.left != null && root.right == null) {
            if (root.left.left == null && root.left.right == null) {
                return true;
            }
            return false;
        }
        if (root.left == null && root.right != null) {
            if (root.right.left == null && root.right.right == null) {
                return true;
            }
            return false;
        }
        return isBalanceCore(root.left) && isBalanceCore(root.right) && root.left.val - root.right.val >= -1 && root.left.val - root.right.val <= 1;
    }
    private TreeNode getDepth(TreeNode root) {
        // TODO Auto-generated method stub
        if (root.left == null && root.right == null) {
            root.val = 1;
        }else if (root.left != null && root.right == null) {
            root.left = getDepth(root.left);
            root.val = 1 + root.left.val;
        }else if (root.left == null && root.right != null) {
            root.right = getDepth(root.right);
            root.val = 1 + root.right.val;
        }else {
            root.left = getDepth(root.left);
            root.right = getDepth(root.right);
            root.val = 1 + (root.left.val > root.right.val ? root.left.val : root.right.val);
        }
        return root;
    }
}
1ms解法

其实原本这篇博客的题目是从8ms到2ms,就在刚刚写到2ms解法的时候,突然想起来,反正我都要计算树中每个结点为根结点的子树的高度,那么为什么不在计算高度的时候顺便判断一下是不是平衡二叉树呢?

  • 这里我设置了一个全局变量isBalance并初始化为true,每次求完一个结点的value,我都判断一下以它为根的子树是否是平衡二叉树,如果不是,那么修改isBalance的值为false。这样,在对树中每个结点求value之后,就已经知道这棵树是否是平衡二叉树了

代码如下:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    boolean isBalance = true;
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        root = getDepth(root);
        return isBalance;
    }
    private TreeNode getDepth(TreeNode root) {
        // TODO Auto-generated method stub
        if (root.left == null && root.right == null) {
            root.val = 1;
        }else if (root.left != null && root.right == null) {
            root.left = getDepth(root.left);
            root.val = 1 + root.left.val;
            if (root.left.val > 1) {
                isBalance = false;
            }
        }else if (root.left == null && root.right != null) {
            root.right = getDepth(root.right);
            root.val = 1 + root.right.val;
            if (root.right.val > 1) {
                isBalance = false;
            }
        }else {
            root.left = getDepth(root.left);
            root.right = getDepth(root.right);
            root.val = 1 + (root.left.val > root.right.val ? root.left.val : root.right.val);
            if (root.left.val - root.right.val > 1 || root.left.val - root.right.val < -1) {
                isBalance = false;
            }
        }
        return root;
    }
}
### Height-Balanced Binary Tree 与 Self-Balanced Binary Tree 的区别 **Height-Balanced Binary Tree** 是一种二叉树,其定义为:对于树中的每个节点,其左右子树的深度之差不超过1。这种平衡性确保了树的整体高度保持在 $ O(\log n) $ 级别,从而保证了查找、插入和删除操作的时间复杂度接近最优。例如,在 LeetCode 题目中,判断一棵二叉树是否是高度平衡的通常涉及递归计算每个节点的左右子树深度,并检查它们的差异[^1]。 **Self-Balanced Binary Tree** 则是一个更广泛的概念。它不仅要求树的高度平衡,还要求在进行插入或删除操作后,树能够通过特定的旋转操作(如左旋、右旋)自动恢复平衡。常见的自平衡二叉搜索树包括 AVL 树 和 红黑树(Red-Black Tree)。AVL 树是一种特殊的高度平衡二叉搜索树,其每个节点的左子树和右子树的高度差最多为1,并且所有操作(插入、删除)都会维持这一性质;而红黑树则通过颜色规则来保证树的大致平衡,虽然它的高度可能略高于 AVL 树,但其插入和删除操作的性能更好[^3]。 #### 关键区别 1. **定义上的区别**: - Height-Balanced Binary Tree 只要求任意节点的左右子树深度差不超过1。 - Self-Balanced Binary Tree 不仅要求高度平衡,还需要支持动态操作(插入、删除)后的自动平衡维护。 2. **应用场景**: - Height-Balanced Binary Tree 通常用于静态结构或不需要频繁更新的场景。 - Self-Balanced Binary Tree 更适合需要频繁插入和删除的动态数据结构,例如数据库索引和语言标准库中的有序集合。 3. **实现机制**: - Height-Balanced Binary Tree 的实现较为简单,只需检查每个节点的子树深度差即可。 - Self-Balanced Binary Tree(如 AVL 树)则需要额外的旋转操作来维持平衡,例如 AVL 树的单旋转和双旋转[^3]。 4. **性能特性**: - 在查找操作较多的情况下,Height-Balanced Binary Tree 和 Self-Balanced Binary Tree 的性能相近。 - 在插入和删除操作较多的情况下,Self-Balanced Binary Tree(如红黑树)通常表现更好,因为它们的平衡策略减少了旋转的次数。 ### 示例代码:AVL 树的插入操作 以下是一个 AVL 树的插入操作示例,展示了如何通过旋转保持树的平衡: ```cpp struct Node { int key; Node *left; Node *right; int height; }; int height(Node *N) { if (N == NULL) return 0; return N->height; } Node* newNode(int key) { Node* node = new Node(); node->key = key; node->left = NULL; node->right = NULL; node->height = 1; return node; } Node* rightRotate(Node *y) { Node *x = y->left; Node *T2 = x->right; x->right = y; y->left = T2; y->height = max(height(y->left), height(y->right)) + 1; x->height = max(height(x->left), height(x->right)) + 1; return x; } Node* leftRotate(Node *x) { Node *y = x->right; Node *T2 = y->left; y->left = x; x->right = T2; x->height = max(height(x->left), height(x->right)) + 1; y->height = max(height(y->left), height(y->right)) + 1; return y; } int getBalance(Node *N) { if (N == NULL) return 0; return height(N->left) - height(N->right); } Node* insert(Node* node, int key) { if (node == NULL) return newNode(key); if (key < node->key) node->left = insert(node->left, key); else if (key > node->key) node->right = insert(node->right, key); else return node; node->height = 1 + max(height(node->left), height(node->right)); int balance = getBalance(node); if (balance > 1 && key < node->left->key) return rightRotate(node); if (balance < -1 && key > node->right->key) return leftRotate(node); if (balance > 1 && key > node->left->key) { node->left = leftRotate(node->left); return rightRotate(node); } if (balance < -1 && key < node->right->key) { node->right = rightRotate(node->right); return leftRotate(node); } return node; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值