Skip to content

Files

Latest commit

 

History

History
42 lines (32 loc) · 982 Bytes

postorder successor of a node in binary tree.md

File metadata and controls

42 lines (32 loc) · 982 Bytes
Screenshot 2023-12-08 at 6 53 08 PM
class TreeNode {
    constructor(value) {
        this.value = value;

        this.left = null;
        this.right = null;
    }
}

// Function to find the next node in postorder traversal
function findNextNodeInPostorder(root, value) {
    let lastVisited = null;
    let nextNode = null;
    let found = false;

    function traverse(node) {
        if (node === null || found) {
            return;
        }

        traverse(node.left);
        traverse(node.right);

        // Check if the last visited node is the one we're looking for
        if (lastVisited && lastVisited.value === value) {
            nextNode = node;
            found = true;
            return;
        }

        lastVisited = node;
    }

    traverse(root);
    return nextNode ? nextNode.value : null;
}