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

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 พ.ย. 2024

ความคิดเห็น • 71

  • @Devildriver4441
    @Devildriver4441 ปีที่แล้ว +12

    crystal clear explanation.Thank you bhaiya

  • @somakbhuti9501
    @somakbhuti9501 2 หลายเดือนก่อน +1

    Parent pointer wala intuition 🔥

  • @kapilrules
    @kapilrules 2 หลายเดือนก่อน +1

    you are simple awesome MIK

    • @kapilrules
      @kapilrules 2 หลายเดือนก่อน

      solved using python by your approach
      # Definition for a binary tree node.
      # class TreeNode:
      # def __init__(self, x):
      # self.val = x
      # self.left = None
      # self.right = None
      class Solution:
      def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]:
      res = []
      parent = {}
      visited = set()
      def preorder(node):
      if node is None:
      return
      if node.left:
      parent[node.left] = node
      preorder(node.left)
      if node.right:
      parent[node.right] = node
      preorder(node.right)
      preorder(root)
      q = collections.deque()
      q.append(target)
      while q:
      if not k:
      break
      for _ in range(len(q)):
      node = q.popleft()
      visited.add(node.val)
      if node.left and node.left.val not in visited:
      q.append(node.left)
      if node.right and node.right.val not in visited:
      q.append(node.right)
      if node in parent and parent[node].val not in visited:
      q.append(parent[node])
      k -= 1
      while q:
      res.append(q.popleft().val)
      return res

  • @dikshantyadav1110
    @dikshantyadav1110 ปีที่แล้ว +1

    Nice explanation, keep going

  • @alphadrones23
    @alphadrones23 ปีที่แล้ว +1

    This was asked to me in Amazon interview

  • @sachinmohanty4577
    @sachinmohanty4577 ปีที่แล้ว +9

    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 ❤️❤️

  • @Tanishq181
    @Tanishq181 11 หลายเดือนก่อน +1

    Thank you so much ❤

  • @pawankumaryadav8127
    @pawankumaryadav8127 4 หลายเดือนก่อน +1

    Beautiful explanation 😊

  • @AbhijeetMuneshwar
    @AbhijeetMuneshwar 5 หลายเดือนก่อน +1

    So intuitive !!! 💡

  • @kartikrameshchavan4710
    @kartikrameshchavan4710 ปีที่แล้ว +4

    Thanks Sir 🙏

  • @manavrawat4877
    @manavrawat4877 หลายเดือนก่อน +1

    bhiya pls contest ki bhi vedio bnao

    • @codestorywithMIK
      @codestorywithMIK  หลายเดือนก่อน

      Sure. I will be back to India next week.
      Let me try to put more of contests

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 ปีที่แล้ว +1

    Good explanation with dryrun

  • @Rider12374
    @Rider12374 6 วันที่ผ่านมา +2

    /**
    * 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:
    unordered_map parent;
    void inorder(TreeNode* root){
    if(root==NULL){
    return ;
    }
    if(root->left){
    parent[root->left]=root;

    }
    inorder(root->left);
    if(root->right){
    parent[root->right]=root;

    }
    inorder(root->right);
    }
    void bfs(TreeNode* target, int k,vector &res ){
    queue q;
    q.push(target);
    unordered_set visited;
    visited.insert(target->val);
    while(!q.empty()){
    int size = q.size();
    if(k==0) break;
    while(size--){
    TreeNode* curr = q.front();
    q.pop();
    if(curr->left && !visited.count(curr->left->val)){
    q.push(curr->left);
    visited.insert(curr->left->val);
    }
    if(curr->right && !visited.count(curr->right->val)){
    q.push(curr->right);
    visited.insert(curr->right->val);
    }
    if(parent.count(curr) && !visited.count(parent[curr]->val)){
    q.push(parent[curr]);
    visited.insert(parent[curr]->val);
    }
    }
    k--;
    }
    while(!q.empty()){
    TreeNode* temp = q.front();
    q.pop();
    res.push_back(temp->val);
    }
    }
    vector distanceK(TreeNode* root, TreeNode* target, int k) {
    vector res;
    inorder(root);
    bfs(target,k, res);
    return res;
    }
    };

  • @anuppatankar4294
    @anuppatankar4294 ปีที่แล้ว +2

    Great Explanation 👌🏻

  • @tutuimam3381
    @tutuimam3381 ปีที่แล้ว +1

    Thanks a lot

  • @abc-ym4zs
    @abc-ym4zs ปีที่แล้ว +3

    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

  • @akshatdubey4421
    @akshatdubey4421 4 หลายเดือนก่อน +2

    The way you explained this is so simple and intuitive. Thanks a lot

  • @ugcwithaddi
    @ugcwithaddi ปีที่แล้ว +1

    Best explanation

  • @wearevacationuncoverers
    @wearevacationuncoverers ปีที่แล้ว +6

    You have the skill to make things look like a CAKE WALK ❤
    I still am so glad that I found this channel

  • @SaranshKasliwal
    @SaranshKasliwal 4 หลายเดือนก่อน

    @codestorywithMIK Can you upload Video on the other approach to solve this question

  • @MohdDanish-j7p
    @MohdDanish-j7p 10 หลายเดือนก่อน +1

    Awesome explanation 👍👍👍

    • @codestorywithMIK
      @codestorywithMIK  10 หลายเดือนก่อน

      Glad you liked it 👍🏻🙏

  • @unodiscite
    @unodiscite ปีที่แล้ว

    inorder traversal ( left root node)

  • @abinashdash7864
    @abinashdash7864 10 หลายเดือนก่อน

    Please make a video without using parent pointer

  • @floatingpoint7629
    @floatingpoint7629 ปีที่แล้ว +1

    thanks for the explanation. i was able to solve this one by self 😄

  • @killshot57
    @killshot57 ปีที่แล้ว +2

    Dp concept and qns series ko please continue kardo 🥺🥺

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว +1

      Yes yes.Coming today/tomorrow ❤️❤️🙏🙏

  • @_singh516
    @_singh516 ปีที่แล้ว +1

    more intutive is Postorder

  • @viditvaish7317
    @viditvaish7317 9 หลายเดือนก่อน +1

    asusual no one can beat ypu in explaining the concept ans dsa question

  • @GeneralistDev
    @GeneralistDev ปีที่แล้ว +3

    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  ปีที่แล้ว +1

      That’s absolutely an acceptable approach.
      Also leetcode has put this in their official solution

  • @vineetkumar2899
    @vineetkumar2899 ปีที่แล้ว +2

    I think bhaiya interviewer will not be happy with this approach 😢.Please make video on second approach soon.

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      @@codestorywithMIK got it. looking forward to that solution 😀

    • @vineetkumar2899
      @vineetkumar2899 ปีที่แล้ว +1

      I'm waiting for that 😁

  • @thekindspill
    @thekindspill ปีที่แล้ว +1

    Made it a cake walk ❤❤

  • @krishnaharshith8120
    @krishnaharshith8120 ปีที่แล้ว +1

    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

  • @codeandtalk6
    @codeandtalk6 ปีที่แล้ว +1

  • @blackstargaming3678
    @blackstargaming3678 ปีที่แล้ว

    bhai please approach 2 jaldi le lao please request he

  • @schrodingerskatt222
    @schrodingerskatt222 10 หลายเดือนก่อน

    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.

  • @kamranwarsi12b22
    @kamranwarsi12b22 ปีที่แล้ว +1

    Can we store the parent mapping using level order traversal ??

  • @uditpanjiyar3203
    @uditpanjiyar3203 2 หลายเดือนก่อน

    bhai itna dimag kaisai lagaya first attempt mai toh mere sai kabhi bhhi nahi hota

  • @SteveBClark
    @SteveBClark หลายเดือนก่อน

    Can anyone share the bfs video where he talks about the bfs logic when to use which bfs techniques

  • @krishanapoorv7710
    @krishanapoorv7710 ปีที่แล้ว +1

    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);
    }
    };

  • @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

  • @husler7424
    @husler7424 ปีที่แล้ว

    Can u please create Recursion playlist?

  • @shivanshnegi6957
    @shivanshnegi6957 ปีที่แล้ว +1

    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 ❤️❤️❤️

  • @RajGupta-cu9hi
    @RajGupta-cu9hi 10 หลายเดือนก่อน

    Where is the approach 2

  • @souravjoshi2293
    @souravjoshi2293 ปีที่แล้ว +2

    Wow itna easy tha kya ye 😥

  • @xiaoshen194
    @xiaoshen194 ปีที่แล้ว +2

    Thnk u for the soln. Main to pta nhi kya tatti approach laga rha tha level order traversal ke sath 🥲😞