two if cond. between (86 and 94).
to be specific , this
leftans!=NULL
two if cond. between (86 and 94).
to be specific , this
leftans!=NULL
@premang these lines check whether you already found LCA on left/right subtree of your current node.
Also leftans!=NULL is used to avoid any kind of segmentation fault as later we check for leftans->node. (since NULL->node makes no sense)
ok thenks i’m satisfied with the answer
lets say this is the tree ; https://media.geeksforgeeks.org/wp-content/cdn-uploads/lca.png
we want to find LCA for (2,7).
1.root is not null
2.custom *leftans=helper(root->left,p,q); <- whii it traverse to the node 4 and return null or what ?
See it goes this way
First at root(1)-> goes to left(2) and finds its equal to p! -> calls left(4) and right(5) in search of q(7) but couldn’t find, so 2 returns a custom node with leftans as true,
similarly from root(1)-> goes to right(3) -> calls left and right, got 7 only … so 3 returns a custom node with rightans = true, so now at 1, we have leftans=true from left subtree and rightans=true from right sub tree, so LCA is 1.
If you still have problem, I suggest you go through lecture once more.
in what you just replied :
First at root(1)-> goes to left(2) and finds its equal to p! -> calls left(4) and right(5) in search of q(7) but couldn’t find, so 2 returns a custom node with leftans as true,.
1 2 4 5 and direct 7 why 3 is not included ?
3 is included! but its from right call!
7 cannot be visited without visiting 3!