Tree: Height of a Binary Tree

  • + 1 comment

    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);
    }