Back to Browse

Step-By-Step Directions From a Binary Tree Node to Another | 2 Approaches | Leetcode 2096

12.6K views
Jul 16, 2024
34:05

Whatsapp Community Link : https://www.whatsapp.com/channel/0029Va6kVSjICVfiVdsHgi1A This is the 46th Video of our Playlist "BINARY TREE : Popular Interview Problems" by codestorywithMIK In this video we will try to solve a very good Tree Problem : Step-By-Step Directions From a Binary Tree Node to Another| 2 Approaches | Leetcode 2096 | 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 : Step-By-Step Directions From a Binary Tree Node to Another| 2 Approaches | Leetcode 2096 | codestorywithMIK Company Tags : AMAZON My solutions on Github(C++ & JAVA) : https://github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Tree/Step-By-Step%20Directions%20From%20a%20Binary%20Tree%20Node%20to%20Another.cpp Leetcode Link : https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another 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 ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗ ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣ ╠╗║╚╝║║╠╗║╚╣║║║║║═╣ ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝ Summary : Approach 1 (Using LCA) Time Complexity: O(n) Space Complexity: O(n) Description: Lowest Common Ancestor (LCA): Find the LCA of the startValue and destValue nodes using a recursive approach. The LCA is the node that is an ancestor to both target nodes and is farthest from the root. Find Paths: From the LCA, find paths to both startValue and destValue. These paths are recorded as sequences of 'L' (left) and 'R' (right). Construct Result: Convert the path from LCA to startValue to 'U' (upward moves) and append the path from LCA to destValue directly to form the result. Steps: Find the LCA of startValue and destValue. Find the path from the LCA to startValue and destValue. Construct the result by moving up from startValue to LCA and then following the path to destValue. Approach 2 (Without finding LCA) Time Complexity: O(n) Space Complexity: O(n) Description: Find Paths: Find paths from the root to both startValue and destValue directly. These paths are recorded as sequences of 'L' (left) and 'R' (right). Identify Common Path: Determine the common path prefix between the two paths. Construct Result: From the point where the paths diverge, convert the path from the common ancestor to startValue to 'U' (upward moves) and append the remaining path to destValue directly to form the result. Steps: Find the path from the root to startValue and destValue. Identify the length of the common path prefix. Construct the result by moving up from startValue to the common ancestor and then following the path to destValue. Both approaches efficiently determine the sequence of moves required to navigate from startValue to destValue in a binary tree, with the first approach leveraging the concept of LCA and the second approach avoiding explicit LCA computation by directly comparing paths from the root. ✨ 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

Download

0 formats

No download links available.

Step-By-Step Directions From a Binary Tree Node to Another | 2 Approaches | Leetcode 2096 | NatokHD