Lowest Common Ancestor of a Binary Tree | Tree Data Structure playlist C++ | Hello World | Hindi
Vložit
- čas přidán 31. 07. 2021
- This is the video under the series of DATA STRUCTURE & ALGORITHM in a TREE Playlist. We are going to understand How to Find the Lowest Common Ancestor of a Binary Tree. We understand How to use the Height of the Binary for Finding the Diameter of the Binary Tree.
Join My Telegram channel for more Updates: telegram.me/helloworldbyprince
► 236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.
We also Provide courses on Competitive Programming and Data structure and Algorithms. Please see our Full Playlist on our Channel.
----------------------------------------------------------------------------------------:
Lowest Common Ancestor of a Binary Tree: leetcode.com/problems/lowest-...
code in This Video: github.com/Prince-1501/Hello_...
----------------------------------------------------------------------------------------
*Follow me *
LinkedIn► / iamprince
Facebook► / helloworldofficials
Instagram► / helloworldbyprince
Twitter► / prince_king_
Telegram► telegram.me/helloworldbyprince
----------------------------------------------------------------------------------------
►Our Playlists on:-
► Tree: • Tree Data Structure & ...
► Hashing: • Hashing Data Structure...
► Matrix: • Matrix (Multidimension...
► STL: • Standard Template Libr...
► Leetcode: • LeetCode Solutions And...
►Competitive Programming: • Full course in Competi...
►C++ Full Course : • C++ full Course in HINDI
►Algorithms: • L-01 || Prefix Sum Arr...
►Data Structure: • Data Structures with C...
------------------------------------------------------------------------
🌟 Please leave a LIKE ❤️ and SUBSCRIBE for more AMAZING content! 🌟
✨ Tags ✨
Lowest Common Ancestor of a Binary Tree
The tree Data structure in Hindi
How to find the LCA of two nodes
Lowest Common Ancestor
Use of Tree Data structure in real Life
question asked in Google
off-campus placement
how to learn to code for beginners
Practice Tree data structure
tree in data structure
Best Telegram channel for Off-campus Placement drive
Tree in a data structure in Hindi
Tree Full playlist for Beginners
#Tree #Leetcode #programming
Itne saare videos dekhne ke baad ...sirf isi me smjh aaya ...finally ...thank u sir ji...
Your videos are very helpful....plese never stop this series 🙏🙏
Ekdum sure Saurabh
@@HelloWorldbyprince 😍💝
Prince sir videos thoda speed se upload karo. Daily ek video toh atleast kar diya karo. By the way awesome content next Aditya verma of tree 👍🏻
Aditya Verma is legend priya 🙂
Agar aap ka bhai ek din google m bhi hogya na selection tho koi hairani ki baat nhi hogi
bhot accha samjha the ho aap
code ka hr ek line k bhot acche se explain krte ho
Please make a series of BST also. 🙏🙏
Great content
ok sure i will uupload sonn
you the best bhaiya
op sir
Very nicely explained bhaiya solution aache se samj me aaya hai your way of explanation is amazing
Thanks a lot lokesh
really thankyou bhaiya apke explanation ke liye
Thanks buddy
sir plz make this type of awesome without worrying views and subscriber
Best explanation of this code ever
Thanks a lot buddy
make video on bst also bhaiya
Very nicely explained, bhaiya, thank you so much
Keep learning buddy 😊
very well explained!!
Glad it was helpful!
You are just best.
Thanks a lot for your support
Plz make more such videos on trees ☺️
sure muskan thanks for the support
Node* LCA(Node *root, int n1, int n2)
{
Node* ans=root;
int k=root->data;
if((n1==k) || (n2==k) || (n1k) ||(n1>k && n2k)
LCA(root->right,n1,n2);
}
nice buddy
path. pop_back()
Bool wale function me end me ise use kyu kiye ho bhaiya
bhai youtube pr bhot sari videos hain but mai apke videos pr kyon ata hoon maloon hai?/KYONKI AP SAMJHA NE KE LIYE VIDEOS BNATE HO .... AUR SAB CZcams KO .....POCKET MONEY KAMANE KA ZARIYA SMJHTE HAIN
waoooo thanks a lot yaar ❤️
//Khud kya hai bhai ,Thank you for making us believe in trying it ourselves
int funct(TreeNode*root,TreeNode*p,vector&arr){
if(root==NULL)return 0;
arr.push_back(root);
if(root->val==p->val)return 1;
else{
if(funct(root->left,p,arr))return 1 ;
if(funct(root->right,p,arr))return 1;
}
arr.pop_back();
return 0;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
vectorarr1;
vectorarr2;
funct(root,p,arr1);
funct(root,q,arr2);
int n=arr1.size(),m=arr2.size();
n= (n>m)?n:m;
int i;
for(i=0;ival!=arr2[i]->val)break;
}
return arr1[i-1];
}
Thoda jyada ho gaya hoga bt I'll try to improve
arr. Pop_back kyu kiye ho bhai
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root) return NULL;
if(root->val== p->val || root->val== q->val) return root;
TreeNode* left = lowestCommonAncestor(root->left,p,q);
TreeNode* right =lowestCommonAncestor(root->right,p,q);
if(left && right) return root;
else if(left && !right) return left;
else if(!left && right) return right;
else return NULL;
}
};
Bhiya aapne bahot aacha smjhaya mgr dimag kharab ho gya
.
Koi baat nahi
Dhere dhere samjh me aayega yaar
Ekdum se pressure mat lo