Tree: Height of a Binary Tree

Sort by

recency

|

985 Discussions

|

  • + 0 comments

    Solution in Java:

    public static int height(Node root) {
          	// Write your code here.
            if (root == null) {
                return -1;
            } else {
                return 1 + Math.max(height(root.left), height(root.right));
            }
        }
    
  • + 0 comments

    Swift is in fact broken. The boilerplate provided is causing issue even with correct code in the getHeight() function. The boilerplate provided is force-unwrapping data in an unsafe way causing the issue to occur during the construction of the tree - even before our user controlled code can execute. Leetcode never has this issue.

  • + 0 comments

    def height(root): left = root.left right = root.right if left == None and right == None: return 0 r_ht, l_ht = 0, 0 if left!=None: l_ht = height(left) if right!=None: r_ht = height(right) return 1+max(r_ht,l_ht)

  • + 0 comments

    Javascript testing doesn't work. Do not waste time on it.

  • + 0 comments

    swift broken, returning 0 with no logic will still crash with unwrapping nil.