[Algorithm-LeetCode] 110.Balanced Binary Tree (Recursive)

Keywords: Javascript Algorithm data structure leetcode Binary tree

110.Balanced Binary Tree-Force Button (LeetCode)

Release: October 10, 2021 21:30:53

Problem description and examples

Given a binary tree, determine whether it is a highly balanced binary tree.

In this topic, a height-balanced binary tree is defined as:
The absolute difference between the left and right subtrees of each node of a binary tree is 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

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

Source: LeetCode
Links:https://leetcode-cn.com/problems/balanced-binary-tree
Copyright belongs to the withholding network. For commercial reprinting, please contact the official authorization. For non-commercial reprinting, please indicate the source.

My Problem (Map)

The first solution you came up with was to recursively calculate the height of the left and right subtrees of a binary tree, and then return the final result based on the difference.

Attempts before success

Following the ideas above, I have written the following program:

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isBalanced = function(root) {
  if(!root) {
    return true; 
  }
  return  Math.abs(getHeight(root.left) - getHeight(root.right)) > 1 ? false : true; 

  function getHeight(root) {
    if(!root) {
      return 0;
    }
    return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
  }
};


Submit Records
 Execution result: Answer error
 Pass test case: 203 / 228
 Input:[1,2,2,3,null,null,3,4,null,null,4]
Output: true
 Expected results: false
 Time: 2021/10/10 21:35

Several of the provided test cases passed, and then I submitted them confidently, and the results...did not pass as expected...The last test case, for example:

Left and right subtrees of the root node are also probably unbalanced binary trees, not considered

The program above only considers the height difference between the left and right subtrees of the root node, but although the height difference between the left and right subtrees of the root node of the above binary tree is less than 2, the height difference between the left and right subtrees of the binary tree rooted by the left child of the root node is greater than or equal to 2. Obviously, this is not a balanced binary tree, but the program only makes one level of judgment, so it returns true.

That is, the program above can only determine that the height difference between the left and right subtrees of the root node can not exceed 2, but it cannot guarantee that the binary tree is a balanced binary tree.

Recursive Solution

After some deliberation, I think the cause of the program error is that there is no deep application of the criteria for balanced binary trees, so only a part of the use cases can be satisfied. So a recursive function getHeight() is needed to calculate the height of the binary trees.Adds a judgment whether the current binary tree is a balanced binary tree, and returns the result to the previous level of recursion by returning a value.

Also note a transitive relationship: if a subtree is not a balanced binary tree, then the subtree in which the subtree is located must not be a balanced binary tree.

So a flag is needed to determine if the current subtree is a balanced binary tree. I identify whether the current subtree is a balanced binary tree by returning -1.

So the return value of the getHeight() function has two cases: if it is a balanced binary tree, it returns its height, if it is not, it returns -1, so at the end, you just need to decide if the return value of the getHeight() function is -1.

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isBalanced = function(root) {
  return getHeight(root) === -1 ? false : true;

  function getHeight(root) {
    if(!root) {
      return 0;
    }
    let leftHeight = getHeight(root.left);
    if(leftHeight === -1) {
      return -1;
    }
    let rightHeight = getHeight(root.right);    
    if(rightHeight === -1) {
      return -1;
    }
    return Math.abs(leftHeight - rightHeight) > 1 ? -1 : Math.max(leftHeight, rightHeight) + 1;
  }
};


Submit Records
 Execution Result: Passed
 Pass test case: 228 / 228
 Execution time: 84 ms, At all JavaScript 67 defeated in submission.39%Users
 Memory consumption: 42.3 MB, At all JavaScript 35 beaten in submission.63%Users
 Time: 2021/10/10 23:05

Official Questions

Update: July 29, 2021 18:43:21

Because I'm concerned about copyright ownership, I no longer paste specific codes in some of the Official Titles, so I can see them in the link below.

Update: October 10, 2021 23:10:42

Reference resources: Balanced Binary Tree-Balanced Binary Tree-Force Button (LeetCode)

[End of update]

Reference

Update: October 10, 2021 23:55:23
Reference resources: [WeChat Public Number: Code Pickup 2020-10-02] Binary Tree: Do I balance?

Posted by lox on Sun, 10 Oct 2021 10:19:23 -0700