[LeetCode] 110. Balanced binary tree (same sword refers to Offer 55-II)

1. Topic

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.

Example 1:
1

输入:root = [3,9,20,null,null,15,7]
输出:true

Example 2:
2

输入:root = [1,2,2,3,3,null,null,4,4]
输出:false

Example 3:

输入:root = []
输出:true

prompt:

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

Two, solve

The depth of this tree = Max{left subtree depth, right subtree depth} + 1
1

1. Post-order traversal + pruning (from bottom to top)

Ideas:

After traversing the binary tree, return the depth of the subtree from bottom to top. If it is judged that a certain subtree is not a balanced tree, then pruning (not easy to think of at the first time), and directly return upward.

Algorithm flow-recur(root) function:

  • Return value :
  1. When the depth difference between the left/right subtree of the node root <=1: return the depth of the current subtree, that is, the maximum depth of the left/right subtree of the node root+1 (max(left, right) + 1)
  2. When the depth difference of the left/right subtree of the node root is greater than 2: -1 is returned, which means that this subtree is not a balanced tree.
  • Termination conditions :
  1. When the root is empty: it means that the leaf node is crossed, so the height is 0;
  2. When the depth of the left/right subtree is -1: it means that the left/right subtree is not a balanced tree, so pruning, directly returns -1.

isBalanced(root) function:

  • Return value: if recur(root) != -1, it means that the tree is balanced, return true; otherwise, return false.

Code:

class Solution {
    
    
    public boolean isBalanced(TreeNode root) {
    
    
        return recur(root) != -1;
    }

    private int recur(TreeNode root) {
    
    
	    // 1、Current logic
        if (root == null) return 0;
        // 3、Drill down
        int left = recur(root.left);
		// 2、Current logic
        if(left == -1) return -1;
        
        // 3、Drill down
        int right = recur(root.right);
        // 2、Current logic
        if(right == -1) return -1;
        
        return Math.abs(left - right) < 2 ? Math.max(left, right) + 1 : -1;
    }
}

Time complexity: O (n) O(n)O ( n )
space complexity: O (n) O(n)O ( n )

2. Pre-order traversal + judgment depth (from top to bottom)

Ideas:

Construct a function depth(root) to obtain the depth of the current subtree (that is, interview question 55-I. The depth of the binary tree ), abs(depth(root.left) - depth(root.right)) <= 1and judge whether a certain subtree is binary by comparing the depth difference between the left and right subtrees of a certain subtree. Balance tree. If all subtrees are balanced, then the subtrees are balanced.

The more specific process will not be explained, please refer to reference 1. The basics are good, and you can understand it in conjunction with code comments.

Code:

  • version 1:
class Solution {
    
    
    public boolean isBalanced(TreeNode root) {
    
    
	    // 1、Terminator
        if (root == null) return true;
        // 2 && 3、Current logic && Drill down
        return Math.abs(depth(root.left) - depth(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
    }

    private int depth(TreeNode root) {
    
    
        if (root == null) return 0;
        return Math.max(depth(root.left), depth(root.right)) + 1;
    }
}
  • Version 2:
class Solution {
    
    
    public boolean isBalanced(TreeNode root) {
    
    
        return judge(root);
    }

    public boolean judge(TreeNode root) {
    
    
        if (root == null) return true;
        if (Math.abs(height(root.left)-height(root.right)) > 1) return false;
        else return judge(root.left) && judge(root.right);
    }
    public int height(TreeNode root) {
    
    
        if (root == null) return 0;
        return Math.max(height(root.left), height(root.right)) + 1;
    }
}

Time complexity: O (nlogn) O(nlogn)O ( n l o g n )

Full binary tree height complexity O (logn) O(logn)O ( l o g n ) , the number of nodes traversed at each level isn,(n-1)/2*2,(n-3)/4*4,(n-7)/8*8,...,1*(n+1)/2.
Overall time complexity = execution complexity of each layer * number of layers complexity =O (nlogn) O(nlogn)O ( n l o g n )

Space complexity: O (n) O(n)O ( n )
2

Three, reference

1. Interview Question 55-II. Balanced Binary Tree (from bottom to top, top to bottom, clear diagrams)

Guess you like

Origin blog.csdn.net/HeavenDan/article/details/110920874