Sort by

recency

|

492 Discussions

|

  • + 0 comments

    A solution in JavaScript:

    this.getHeight = function(root) {
      let height = 0;
      const traverse = (leaf, depth) => {
        if (depth > height) {
          height = depth;
        }
        if (leaf.left) {
          traverse(leaf.left, depth + 1);
        }
        if (leaf.right) {
          traverse(leaf.right, depth + 1);
        }
      }; 
      if (root) {
        traverse(root, 0);
      }
      return height;
    };
    
  • + 0 comments
        def getHeight(self,root):
            left_height = 0 if root.left is None else 1 + self.getHeight(root.left)
            right_height = 0 if root.right is None else 1 + self.getHeight(root.right)
                   
            return max(left_height, right_height)
    
  • + 0 comments

    My head really exploded and it didn't even tell me to build the BST class...

  • + 0 comments

    Javascript code:

    // Start of function getHeight
    this.getHeight = function(root) {
    
        if (root===null) return -1;
        leftheight=this.getHeight(root.left);
        rightheight=this.getHeight(root.right);
        return Math.max(leftheight,rightheight)+1;
    
    }; // End of function getHeight
    

    Don't understand how the output get 2 with the below input. Any expert can help to shed some light please. Thanks!

    9 20 50 35 44 9 15 62 11 13

  • + 0 comments

    Python 3 solution

    python def getHeight(self,root):

        if root.left:
            left = self.getHeight(root.left) + 1
        else:
            left = 0
        if root.right:
            right =self.getHeight(root.right) +1
        else:
            right = 0
    
        return max(left, right)