sir why these fail in coding blocks ide rather it works fine in
sublime https://ide.codingblocks.com/s/257786
Lowest common ancestor using list
@YASHMATHURIA123 hey isk solution ka dusra efficient version bhi hai jisme extra space nhi lgta like isme list use horhi hai ,to constraints ki wjah se may be submit na horha ho,woh wala solution try kro.
No sir abhi m bas coding blocks ide pe chalaa raha hoon aur is progeam m ek dikhaat hai ki jab ye program m mai d variablr ko global declare karta hoon toh woh chaltaa hai magar jab mai d ko main m declare karta hoon woh nahi chaltaa code output nahi dega
@YASHMATHURIA123 hey I think your logic is incorrect,can you explain me your logic,as the logic explained in LCA 1 is not like that.
Sir first i call recursion and make list of ancestor of p and again i call recursion it make list of ancestor of q then i check common elements in both list and thats my answer
@YASHMATHURIA123 hey apka logic sahi nhi hai root to particular node path find krne ka ,backtracking bhi lgegi ,check this code:
// Finds the path from root node to given root of the tree, Stores the
// path in a vector path[], returns true if path exists otherwise false
bool findPath(Node *root, vector< int > &path, int k)
{
// base case
if (root == NULL) return false ;
// Store this node in path vector. The node will be removed if
// not in path from root to k
path.push_back(root->key);
// See if the k is same as root's key
if (root->key == k)
return true ;
// Check if k is found in left or right sub-tree
if ( (root->left && findPath(root->left, path, k)) ||
(root->right && findPath(root->right, path, k)) )
return true ;
// If not present in subtree rooted with root, remove root from
// path[] and return false
path.pop_back();
return false ;
}