DEV Community

hwangs12
hwangs12

Posted on

Binary tree - js

Depth First Values


const depthFirstValues = (root) => {

    if (root === null) return [];

    const stack = [root];

    const result = [];

    while (stack.length > 0) {
        const current = stack.pop();

        result.push(current.val);

        if (current.right) stack.push(current.right);
        if (current.left) stack.push(current.left);

    }

    return result;

}


Enter fullscreen mode Exit fullscreen mode

Top comments (0)