We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a3e85c6 commit 57d7cddCopy full SHA for 57d7cdd
17 - Binary Tree Data Structure Problems/25 - LCA in Binary Tree/main.cpp
@@ -0,0 +1,19 @@
1
+class Solution
2
+{
3
+ public:
4
+ //Function to return the lowest common ancestor in a Binary Tree.
5
+ Node* lca(Node* root ,int n1 ,int n2 )
6
+ {
7
+ if(root == NULL) return NULL;
8
+
9
+ if(root -> data == n1 || root -> data == n2) return root;
10
11
+ Node* leftAns = lca(root -> left, n1, n2);
12
+ Node* rightAns = lca(root -> right, n1, n2);
13
14
+ if(leftAns != NULL && rightAns != NULL) return root;
15
+ else if(leftAns != NULL && rightAns == NULL) return leftAns;
16
+ else if(leftAns == NULL && rightAns != NULL) return rightAns;
17
+ else return NULL;
18
+ }
19
+};
0 commit comments