All Nodes Distance K in Binary Tree | Approach-1 | Leetcode-863 | AMAZON | Explanation ➕ Live Coding

  Рет қаралды 6,322

codestorywithMIK

codestorywithMIK

Күн бұрын

Hi everyone, this is the 29th video of our "Binary Tree" Playlist.
In this video we will try to solve a very famous Binary Tree Qn “All Nodes Distance K in Binary Tree”.
We will write very easy and clean code.
We will do live coding after explanation and see if we are able to pass all the test cases.
In this video, we will solve it using Approach-1 (Using Parent Pointers)
Problem Name : All Nodes Distance K in Binary Tree
Company Tags : Amazon, Citicorp, Flipkart, Goldman Sachs, Hike, Ola Cabs, Samsung, Walmart
My solutions on Github : github.com/MAZ...
Leetcode Link : leetcode.com/p...
My GitHub Repo for interview preparation : github.com/MAZ...
Subscribe to my channel : / @codestorywithmik
Instagram : / codestorywithmik
Facebook : / 100090524295846
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
#interviewpreparation #interview_ds_algo #hinglish

Пікірлер: 63
@Devildriver4441
@Devildriver4441 Жыл бұрын
crystal clear explanation.Thank you bhaiya
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Bishal ❤️
@sachinmohanty4577
@sachinmohanty4577 Жыл бұрын
I woke up in the morning attempted this ..i was so out of my mind ki mei queue soch ke stack ke hisab se pop kar raha hun dry run mei 😂😂 then finally samjha ki kitna bada blunder kar raha tha ... Thanks MIK ❤
@codestorywithMIK
@codestorywithMIK Жыл бұрын
🤓🤓 Thank you for watching ❤️❤️
@wearevacationuncoverers
@wearevacationuncoverers Жыл бұрын
You have the skill to make things look like a CAKE WALK ❤ I still am so glad that I found this channel
@abc-ym4zs
@abc-ym4zs Жыл бұрын
you have very unique skill to explain very good problems in a very understandable way only one request sir please continue these series of solving dialy problems sir finally a big thanks to you sir whenever i fell demotivated in DSA after seeing ur solutions again i am pumped up to solve questions sir
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much 🙏😇❤️
@akshatdubey4421
@akshatdubey4421 2 ай бұрын
The way you explained this is so simple and intuitive. Thanks a lot
@ntsequalifier341
@ntsequalifier341 Ай бұрын
Really well explained.
@AbhijeetMuneshwar
@AbhijeetMuneshwar 2 ай бұрын
So intuitive !!! 💡
@pawankumaryadav8127
@pawankumaryadav8127 Ай бұрын
Beautiful explanation 😊
@kartikrameshchavan4710
@kartikrameshchavan4710 Жыл бұрын
Thanks Sir 🙏
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you for watching ❤️🙏
@viditvaish7317
@viditvaish7317 6 ай бұрын
asusual no one can beat ypu in explaining the concept ans dsa question
@krishnaharshith8120
@krishnaharshith8120 Жыл бұрын
Hey I have been following ur videos to help me solve my daily leetcode problems, and the way u teach is impressive and it motivates me to do atleast one question everyday. Thanks fror the content you put out, hope you keep doing this and would love to get to interact with you , as a mentor or a guide coz i am pretty confused and unguided all the time. Once again thank you, if you are looking at this comment please do reply, would love to get to message you irl. Thank you
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot 🙏😇❤️ You can connect on LinkedIn
@ugcwithaddi
@ugcwithaddi Жыл бұрын
Best explanation
@xiaoshen194
@xiaoshen194 Жыл бұрын
Thnk u for the soln. Main to pta nhi kya tatti approach laga rha tha level order traversal ke sath 🥲😞
@floatingpoint7629
@floatingpoint7629 Жыл бұрын
thanks for the explanation. i was able to solve this one by self 😄
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Wowieee. Awesome 😍😇🙏
@killshot57
@killshot57 Жыл бұрын
Dp concept and qns series ko please continue kardo 🥺🥺
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Yes yes.Coming today/tomorrow ❤️❤️🙏🙏
@anuppatankar4294
@anuppatankar4294 Жыл бұрын
Great Explanation 👌🏻
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much Anup ❤️❤️
@krishanapoorv7710
@krishanapoorv7710 Жыл бұрын
I was waiting for your video since morning so that i can get more approaches ,this hash-map approach is everywhere but what if i have to solve this question without hash-map without using parent-child relationship and also not converting it into graph.so can you solve this with another approach. I have doubt in one code can you make me understand how that code is running class Solution { public: vector distanceK(TreeNode* root, TreeNode* target, int k) { vector res; dfs(root, target, k, res); return res; } private: int dfs(TreeNode* node, TreeNode* target, int k, vector& res) { if (!node) { return -1; } if (node == target) { subtreeAdd(node, 0, k, res); return 0; } int left = dfs(node->left, target, k, res); int right = dfs(node->right, target, k, res); if (left != -1) { if (left + 1 == k) { res.push_back(node->val); } subtreeAdd(node->right, left + 2, k, res); return left + 1; } if (right != -1) { if (right + 1 == k) { res.push_back(node->val); } subtreeAdd(node->left, right + 2, k, res); return right + 1; } return -1; } void subtreeAdd(TreeNode* node, int dist, int k, vector& res) { if (!node) { return; } if (dist == k) { res.push_back(node->val); } subtreeAdd(node->left, dist + 1, k, res); subtreeAdd(node->right, dist + 1, k, res); } };
@user-og1ss2pv2l
@user-og1ss2pv2l 7 ай бұрын
Awesome explanation 👍👍👍
@codestorywithMIK
@codestorywithMIK 7 ай бұрын
Glad you liked it 👍🏻🙏
@tutuimam3381
@tutuimam3381 Жыл бұрын
Thanks a lot
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
Good explanation with dryrun
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot 🙏😇
@dikshantyadav1110
@dikshantyadav1110 Жыл бұрын
Nice explanation, keep going
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you 🙂❤️
@Tanishq181
@Tanishq181 8 ай бұрын
Thank you so much ❤
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
You're welcome 😊
@GeneralistDev
@GeneralistDev Жыл бұрын
Sir my brute approach was to convert the tree into adj list form since each node->vak is unique . Is it not good for interview ?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
That’s absolutely an acceptable approach. Also leetcode has put this in their official solution
@Momentsofmagic28
@Momentsofmagic28 Жыл бұрын
Made it a cake walk ❤❤
@schrodingerskatt222
@schrodingerskatt222 8 ай бұрын
It would be better to have a hashmap of TreeNodes rather than int, suppose you have repeated numbers at kth distance from target, then it would print it only once.
@codeandtalk6
@codeandtalk6 Жыл бұрын
@SaranshKasliwal
@SaranshKasliwal Ай бұрын
@codestorywithMIK Can you upload Video on the other approach to solve this question
@vineetkumar2899
@vineetkumar2899 Жыл бұрын
I think bhaiya interviewer will not be happy with this approach 😢.Please make video on second approach soon.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Indeed the interviewer will ask for better one. And that’s why I made a separate video on approach-1 Definitely I am gonna make Approach-2 video as well 😇❤️
@floatingpoint7629
@floatingpoint7629 Жыл бұрын
whats wrong with this approach. this is the official approach chosen by leetcode itself
@codestorywithMIK
@codestorywithMIK Жыл бұрын
@floatingpoint7629 It’s not a bad approach. However, it can be solved without using map and parent pointer. So, in case interviewer asks to solve without using parent pointers.
@floatingpoint7629
@floatingpoint7629 Жыл бұрын
@@codestorywithMIK got it. looking forward to that solution 😀
@vineetkumar2899
@vineetkumar2899 Жыл бұрын
I'm waiting for that 😁
@unodiscite
@unodiscite 11 ай бұрын
inorder traversal ( left root node)
@alphadrones23
@alphadrones23 Жыл бұрын
This was asked to me in Amazon interview
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Wow 😯
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Wow itna easy tha kya ye 😥
@abinashdash7864
@abinashdash7864 7 ай бұрын
Please make a video without using parent pointer
@husler7424
@husler7424 Жыл бұрын
Can u please create Recursion playlist?
@kamranwarsi12b22
@kamranwarsi12b22 Жыл бұрын
Can we store the parent mapping using level order traversal ??
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Yes definitely
@kamranwarsi12b22
@kamranwarsi12b22 Жыл бұрын
Ohkk , thnx
@shivanshnegi6957
@shivanshnegi6957 Жыл бұрын
bhaiya dp playlist kaafi time sai update nhi hui plz atleast 1 week mai 3 video daal diya kaaro dp ki 🥲🥲
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Sure thing. Will upload this week ❤️❤️❤️
@SohelKhan-vt5ql
@SohelKhan-vt5ql Жыл бұрын
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void inorder(TreeNode *root , vector adj[]) { if(not root) return; if(root -> left) { adj[root -> left -> val].push_back(root -> val); adj[root -> val].push_back(root -> left -> val); } inorder(root -> left , adj); if(root -> right) { adj[root -> right -> val].push_back(root -> val); adj[root -> val].push_back(root -> right -> val); } inorder(root -> right , adj); } vector distanceK(TreeNode* root, TreeNode* target, int k) { vector adj[501]; inorder(root , adj); vector visited(501 , false); for(int i = 0 ; i < 3 ; i++) { cout
@blackstargaming3678
@blackstargaming3678 Жыл бұрын
bhai please approach 2 jaldi le lao please request he
@RajGupta-cu9hi
@RajGupta-cu9hi 7 ай бұрын
Where is the approach 2
@_singh516
@_singh516 11 ай бұрын
more intutive is Postorder
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you for sharing 😇
Cute kitty gadgets 💛
00:24
TheSoul Music Family
Рет қаралды 22 МЛН
Apple peeling hack
00:37
_vector_
Рет қаралды 120 МЛН
ALL NODES DISTANCE K IN BINARY TREE | PYTHON | LEETCODE # 863
15:41
Cracking FAANG
Рет қаралды 7 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 280 М.
How I Failed the Google Coding Interview (and lessons I learned)
14:24
Cursor Is Beating VS Code (...by forking it)
18:00
Theo - t3․gg
Рет қаралды 42 М.
All nodes distance K in binary tree | Leetcode #863
12:45
Techdose
Рет қаралды 9 М.
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 375 М.
Cute kitty gadgets 💛
00:24
TheSoul Music Family
Рет қаралды 22 МЛН