Smallest String Starting From Leaf | DFS | BFS | Google | Leetcode 988 | codestorywithMIK
Whatsapp Community Link : https://www.whatsapp.com/channel/0029Va6kVSjICVfiVdsHgi1A
This is the 43rd Video of our Playlist "Binary Tree : Popular Interview Problems" by codestorywithMIK
In this video we will try to solve a very good Binary Tree problem : Smallest String Starting From Leaf | DFS | BFS | Google | Leetcode 988 | codestorywithMIK
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 : Smallest String Starting From Leaf | DFS | BFS | Google | Leetcode 988 | codestorywithMIK
Company Tags : will update soon
My solutions on Github(C++ & JAVA) : https://github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Tree/Smallest%20String%20Starting%20From%20Leaf.cpp
Leetcode Link : https://leetcode.com/problems/smallest-string-starting-from-leaf/
My DP Concepts Playlist : https://youtu.be/7eLMOE1jnls
My Graph Concepts Playlist : https://youtu.be/5JGiZnr6B5w
My Recursion Concepts Playlist : https://www.youtube.com/watch?v=pfb1Zduesi8&list=PLpIkg8OmuX-IBcXsfITH5ql0Lqci1MYPM
My GitHub Repo for interview preparation : https://github.com/MAZHARMIK/Interview_DS_Algo
Instagram : https://www.instagram.com/codestorywithmik/
Facebook : https://www.facebook.com/people/codestorywithmik/100090524295846/
Twitter : https://twitter.com/CSwithMIK
Subscribe to my channel : https://www.youtube.com/@codestorywithMIK
Approach Summary :
### Approach 1: Depth-First Search (DFS)
- **Time Complexity (T.C):** O(n) - where n is the number of nodes in the tree. This is because each node is visited exactly once.
- **Space Complexity (S.C):** O(n) - space taken for recursion system stack. In the worst-case scenario, the recursion stack could go as deep as the number of nodes in the tree.
- **Description:**
- The algorithm performs a depth-first traversal of the tree, starting from the root.
- At each node, it appends the character corresponding to the current node's value to the string `curr`.
- If the current node is a leaf node (i.e., it has no children), it compares the resulting string `curr` with the current `result`, updating `result` if necessary.
- Recursively explores the left and right subtrees.
### Approach 2: Breadth-First Search (BFS)
- **Time Complexity (T.C):** O(n) - where n is the number of nodes in the tree. Each node is visited once in the worst-case scenario.
- **Space Complexity (S.C):** O(n) - space used by the queue to store nodes and strings.
- **Description:**
- The algorithm performs a breadth-first traversal of the tree, starting from the root.
- It uses a queue to keep track of nodes and their corresponding strings.
- At each step, it dequeues a node and its associated string from the queue.
- If the dequeued node is a leaf node, it compares the resulting string with the current `result`, updating `result` if necessary.
- Enqueues the left and right children of the dequeued node along with their corresponding strings, obtained by appending the character corresponding to the child node's value to the current string.
Both approaches achieve the same goal of finding the lexicographically smallest string formed by traversing from the root to a leaf node in the binary tree. The choice between the two approaches may depend on factors such as memory constraints or the specific characteristics of the tree.
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
✨ 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 #newyear2024