We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Data Structures
  3. Trees
  4. Tree: Height of a Binary Tree
  5. Discussions

Tree: Height of a Binary Tree

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 864 Discussions, By:

recency

Please Login in order to post a comment

  • l_a_2022
    5 days ago+ 0 comments

    The code provided for Javascript is broken.

    You can copy this wrapper and use typescript. Type your solution inside the treeHeight function.

    'use strict';
    
    process.stdin.resume();
    process.stdin.setEncoding('utf-8');
    // process.stdin.setEncoding('ascii');
    let inputString: string = '';
    let inputLines: string[] = [];
    let currentLine: number = 0;
    process.stdin.on('data', function(inputStdin: string): void {
        inputString += inputStdin;
    });
    
    process.stdin.on('end', function(): void {
        inputLines = inputString.split(/\s/);
        inputString = '';
        solution();
    });
    
    function readLine(): string {
        return inputLines[currentLine++];
    }
    
    class TreeNode {
        data:number;
        left:null|TreeNode;
        right:null|TreeNode;
        constructor(data:number){
        this.data = data;
        this.left = null;
        this.right = null;
        }
    }
    
    class Tree {
        root:TreeNode|null;
        constructor() {
        this.root = null;
        }
        insert(node:TreeNode, data:number) {
            if (node == null){
                node = new TreeNode(data);
            }
            else if (data < node.data){
                node.left  = this.insert(node.left, data);
            }
            else{
                node.right = this.insert(node.right, data);   
            }
    
            return node;
        }
    }
    
    
    // This is a "method-only" submission.
    // You only need to complete this method.
    
    function treeHeight(root:TreeNode):string {
    	return "Your solution in here";
    
    }
    
    
    function solution() {
    
        var tree = new Tree();
        var n = parseInt(readLine());
    
        for (var i=0; i<n; i++) {
            var m = parseInt(readLine());
            tree.root = tree.insert(tree.root, m);
        }
    
        var height = treeHeight(tree.root);
        process.stdout.write(height);
    }
    
    0|
    Permalink
  • newbier
    1 week ago+ 0 comments

    Python 3, breadth-first traversal, without recursion

    def height(root):
        nodes = {0 : [root]}
        h = 0
        while nodes.get(h):
            nodes[h+1] = []
            for node in nodes[h]:
                if node.left:
                    nodes[h+1].append(node.left)
                if node.right:
                    nodes[h+1].append(node.right)
            h += 1
        return max(nodes.keys()) - 1
    
    0|
    Permalink
  • dubeyutkarsh314
    1 week ago+ 0 comments

    simple c++ recursive solution

    int height(Node* root) {
           if(root==NULL){return -1;}
           else{
               int left = height(root->left);
               int right = height(root->right);
               return max(left,right)+1;
           }
        }
    
    0|
    Permalink
  • alannoeortega
    4 weeks ago+ 1 comment

    (JavaScript) Bug report on building a tree. For the example:

    7
    3 5 2 1 4 6 7
    

    It creates an _stdin_array = [ '7', '3 5 2 1 4 6 7' ] but the method readLine() tries to read n lines/elements after reding the first line(_stdin_array[0]) to assign the value to n but we only have one more line/element. Thus, the first node is created correctly because parseInt('3 5 2 1 4 6 7' ); but later tries to get _stdin_array[2] and _stdin_array[3] and so on, elements that not exists in the array and it gets an undefined and parseInt(undefined) returns a NaN. You end up with a right-loaded tree with mostly NaN data values.

    4|
    Permalink
  • uzma_abidi
    1 month ago+ 0 comments

    In Swift

    code gave run time because boilerplate code. Hackerrank must fix this line: " root = tree.insert(root: root, data: Int(readLine()!)!) ".

        func getHeight(root: Node?) -> Int {
            if root == nil {
                    return -1
            } else {
    
                    let left = getHeight(root: root?.left)
                    let right = getHeight(root: root?.right)
    
                    if left > right {
                            return left + 1
                    } else {
                            return right + 1
                    }
             }
        }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy