Tree: Height of a Binary Tree

Sort by

recency

|

968 Discussions

|

  • + 0 comments
    def height(root):
        queue = deque([root])
        height = -1
    
        while queue:
            level_len = len(queue)
            height += 1
    
            for _ in range(level_len):
                current = queue.popleft()
                if current.left:
                    queue.append(current.left)
                if current.right:
                    queue.append(current.right)
        
        return height
    
  • + 0 comments

    Typescript missing boilerplate

  • + 1 comment

    C# missing boilerplate

  • + 0 comments

    PYTHON solution

    def height(root):
        left_h, right_h = 0, 0
        if root.left:
            left_h = 1 + height(root.left)
        if root.right:
            right_h = 1 + height(root.right)
        return max(left_h, right_h)
    
  • + 1 comment

    Feedback: as i am writing right now, the JavaScript (Node.js) environment is broken. We need to switch to another working language environment (like python3).

    Error:

    node:internal/streams/writable:474
    
          throw new ERR_INVALID_ARG_TYPE(
    
          ^
    
    TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined