Solution to Balanced Binary Tree by LeetCode

5 Jul

Question: https://oj.leetcode.com/problems/balanced-binary-tree/

Question Name: Balanced Binary Tree

2 Replies to “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 *

Please put your code into a <pre>YOUR CODE</pre> section. Thanks and Happy Coding!