Count the nodes at distance K from leaf | 2 Approaches | Simple Dry Run | GFG POTD
Whatsapp Community Link : https://www.whatsapp.com/channel/0029Va6kVSjICVfiVdsHgi1A
This is the 39th Video of our Playlist "Binary Tree : Popular Interview Problems".
In this video we will try to solve two very good map problems - Count the nodes at distance K from leaf | GFG POTD
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.
Problem Name : Count the nodes at distance K from leaf | GFG POTD
Company Tags : Flipkart, Microsoft
My solutions on Github(C++ & JAVA) : https://github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Tree/Count%20the%20nodes%20at%20distance%20K%20from%20leaf.cpp
GfG Link : https://www.geeksforgeeks.org/problems/node-at-distance/1
My DP Concepts Playlist : https://youtu.be/7eLMOE1jnls
My Graph Concepts Playlist : https://youtu.be/5JGiZnr6B5w
My GitHub Repo for interview preparation : https://github.com/MAZHARMIK/Interview_DS_Algo
Subscribe to my channel : https://www.youtube.com/@codestorywithMIK
Instagram : https://www.instagram.com/codestorywithmik/
Facebook : https://www.facebook.com/people/codestorywithmik/100090524295846/
Twitter : https://twitter.com/CSwithMIK
Part-1 Summary : The method storeRootToLeaf recursively traverses the tree and populates 'paths' with these paths. In the printKDistantfromLeaf method, it initializes an empty set 'st' and uses 'storeRootToLeaf' to populate 'paths'. It then iterates through each path in 'paths', calculates the distance from the leaf node to the target node (considering 'k' distance), and adds the node at that distance to the set 'st'. Finally, it returns the count of unique nodes in the set 'st', representing the nodes at a distance of 'k' from leaf nodes.
Part-2 Summary : The approach contains a method printKDistantfromLeaf intended to return the count of nodes at a specified distance from leaf nodes in a binary tree. The approach uses a recursive function solve to traverse the binary tree, maintaining a path vector and a set to keep track of nodes at the desired distance from leaf nodes. The method takes a root node, a level, a set for results, a vector for the current path, and an integer 'k' as parameters. The function populates the result set with nodes at the required distance and returns the count.
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
✨ Timelines✨
00:00 - Introduction
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #2024 #newyear