diff --git a/src/day1/BTInOrder.ts b/src/day1/BTInOrder.ts index cf9dbd6..def2d1a 100644 --- a/src/day1/BTInOrder.ts +++ b/src/day1/BTInOrder.ts @@ -1,16 +1,23 @@ -function walk(curr:BinaryNode | null, path:number[]): number[]{ - //Base Case - if(!curr){ +function walk(curr: BinaryNode | null, path: number[]): number[] { + // Base Case: If the current node is null, return the current path (end recursion). + if (!curr) { return path; } - //Recurse + + // Recursive Call: Traverse the left subtree first (In-Order: Left -> Node -> Right). walk(curr.left, path); + + // Process Current Node: Add the current node's value to the path. path.push(curr.value); + + // Recursive Call: Traverse the right subtree. walk(curr.right, path); - //Post + + // Return the path after visiting all nodes. return path; } +// Function to initiate the in-order traversal from the given tree head. export default function in_order_search(head: BinaryNode): number[] { - return walk(head,[]); + return walk(head, []); }