Solution to Balanced Binary Tree by LeetCode

5 Jul


Question Name: Balanced Binary Tree

2 thoughts on “Solution to Balanced Binary Tree by LeetCode

  1. isn’t the requirement just to return a bool ?

    ISBALANCED ; return True
    is NOTBALANCED ; return False

    this program returns a tuple from the Helper functions , and the main function is returning zero’th index element of the tuple. Couldnt we simplify this ?

    • Yes, it is possible to simply the return value. Anyway, the return value of helper should contain two pieces of information:
      1. Whether this (sub)tree is balanced. (Here, we use the first item to represent it.)
      2. If it is balanced, what is the height? (We use the second item to hold it)
      However, the main function only need the first information.

      An alternative solution is to return -1 if it is not balanced, or height otherwise.

Leave a Reply

Your email address will not be published. Required fields are marked *