2
头图

Balanced binary tree

Title description: Given a binary tree, judge whether it is a highly balanced binary tree.

In this question, a highly balanced binary tree is defined as:

The absolute value of the height difference between the left and right subtrees of each node of a binary tree does not exceed 1.

Please refer to LeetCode official website for example description.

Source: LeetCode
Link: https://leetcode-cn.com/problems/balanced-binary-tree/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Solution 1: Recursion
  • First, add a method height, which obtains the height of the binary tree by traversing the hierarchy.
  • Then, the recursive method is used to determine whether the binary tree is a balanced binary tree. The recursive process is as follows:

    • If the current root node is empty, return true directly;
    • Otherwise, calculate the height of the left and right subtrees of the current root node. If the difference between the heights of the left and right subtrees of the current root node does not exceed 1, then recursively judge whether the left and right subtrees of the current root node are balanced binary trees; otherwise, return false.
import com.kaesar.leetcode.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class LeetCode_110 {
    /**
     * 递归
     *
     * @param root
     * @return
     */
    public static boolean isBalanced(TreeNode root) {
        // 如果当前根节点为null,肯定是平衡的,直接返回true
        if (root == null) {
            return true;
        }
        // 如果当前根节点的左右子树的高度之差不超过1,则递归判断当前根节点的左右子树是否是平衡二叉树
        if (height(root.left) - height(root.right) >= -1 && height(root.left) - height(root.right) <= 1) {
            return isBalanced(root.left) && isBalanced(root.right);
        } else {
            return false;
        }
    }

    /**
     * 层序遍历:通过计算二叉树的层数来得到二叉树的高度
     *
     * @param root 当前根节点
     * @return 返回二叉树的高度
     */
    public static int height(TreeNode root) {
        int result = 0;
        if (root == null) {
            return result;
        }
        Queue<TreeNode> nodes = new LinkedList<>();
        nodes.add(root);
        result = 0;
        while (!nodes.isEmpty()) {
            result++;
            int count = nodes.size();
            while (count > 0) {
                TreeNode cur = nodes.poll();
                if (cur.left != null) {
                    nodes.add(cur.left);
                }
                if (cur.right != null) {
                    nodes.add(cur.right);
                }
                count--;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        // 测试用例,期望返回结果: true
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);

        System.out.println(isBalanced(root));
    }
}
[Daily Message] Give all the unhappiness to yesterday, give all the hopes to tomorrow, and give all the efforts to today.

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!