LeetCode-110. Balanced Binary Tree [C++][Java]

LeetCode-110. Balanced Binary Tree [C++][Java]https://leetcode.com/problems/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 left and right subtrees of every node differ in height by no more than 1.

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: true

Example 2:

Input: root = [1,2,2,3,3,null,null,4,4]
Output: false

Example 3:

Input: root = []
Output: true

Constraints:

  • The number of nodes in the tree is in the range [0, 5000].
  • -104 <= Node.val <= 104

解题思路

【C++解法】

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */

1、前序遍历

从上到下遍历,借助一个获取树深度的递归函数,根据该结点的左右子树高度差判断是否平衡,然后递归地对左右子树进行判断。在判断上层结点的时候,会多次重复遍历下层结点,增加了不必要的开销。

class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if(root == NULL) {return true;}
        return abs(dfs(root->left) - dfs(root->right)) <= 1
                && isBalanced(root->left)
                && isBalanced(root->right);
    }

    int dfs(TreeNode* root){
        return root ? 1 + max(dfs(root->left), dfs(root->right)) : 0;
    }
};

2、后序遍历

从下往上遍历,如果子树是平衡二叉树,则返回子树的高度;如果发现子树不是平衡二叉树,则直接停止遍历,这样至多只对每个结点访问一次。

两个关键步骤:

1)检查左右子树高度差;

2)判断子树是否为平衡树。

两个返回值,一个return,一个引用。

class Solution {
public:
    bool isBalanced(TreeNode* root) {
        int depth = 0;
        return isBalanced(root, depth);
    }
    
    bool isBalanced(TreeNode* root, int& depth) {
        if (root) {
            int left = 0, right = 0;
            if (isBalanced(root->left, left) && isBalanced(root->right, right)) {
                int diff = abs(left - right);
                if (diff > 1) {return false;}
                depth = max(left, right) + 1;
                return true;
            } else {return false;}
        } else {return true;}
    }
};

再整洁一点

class Solution {
public:
    bool isBalanced(TreeNode* root) {
        return dfs(root) != -1;
    }

    int dfs(TreeNode* root){
        if (root == nullptr) {return 0;}
        int left = dfs(root->left);
        int right = dfs(root->right);
        if (left == -1 || right == -1 || abs(left-right) > 1) {return -1;}
        else {return 1 + max(left, right);}
    }
};

【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;
 *     }
 * }
 */

1、前序遍历

class Solution {
    public boolean isBalanced(TreeNode root) {
        if (root == null) {return true;}
        return Math.abs(dfs(root.left) - dfs(root.right)) <= 1
               && isBalanced(root.left)
               && isBalanced(root.right);
    }
    
    private int dfs(TreeNode root){
        return root == null ? 0 : 1 + Math.max(dfs(root.left), dfs(root.right));
    }
}

2、后序遍历

class Solution {
    public boolean isBalanced(TreeNode root) {
        return dfs(root) == -1 ? false : true;
    }
    
    int dfs(TreeNode root){
        if (root == null) {return 0;}
        int l = dfs(root.left);
        int r = dfs(root.right);
        if (l == -1 || r == -1) {return -1;}
        if (Math.abs(l-r) > 1) {return -1;}
        return Math.max(l,r) + 1;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫道绝缘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值