• + 0 comments

    Hello everyone!

    I've struggles with solving this problem.

    1) I can't understand the input provided, I assume it's a node with data, left and right properties.

    2) And this is my javascript solution which prints nothing:

    function processData(input) {
    
        function printTopView(root) {
            const result = [];
    
            if (root.left) goLeft(root.left);
    
            result.push(root.data);
    
            if (root.right) goRight(root.right);
    
            console.log(result.join(' '));
    
            function goLeft(node) {
                if (node.left) goLeft(node.left); 
                result.push(node.data);
            }
    
            function goRight(node) {
                result.push(node.data);
                if (node.right) goRight(node.right); 
            }
        }
    
        printTopView(input);
    }