Even Odd Tree | DFS | BFS | Leetcode 1609

codestorywithMIK
codestorywithMIK
3.7 هزار بار بازدید - 5 ماه پیش - Whatsapp Community Link :
Whatsapp Community Link : https://www.whatsapp.com/channel/0029...
This is the 42nd Video of our Playlist "Binary Tree  : Popular Interview Problems".
In this video we will try to solve a very good Binary Tree problem : Even Odd Tree | Leetcode 1609

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 : Even Odd Tree | DFS | BFS | Leetcode 1609
Company Tags  : AMAZON
My solutions on Github(C++ & JAVA)  : https://github.com/MAZHARMIK/Intervie...
Leetcode Link  : https://leetcode.com/problems/even-od...


My DP Concepts Playlist : Roadmap for DP | How to Start DP ? | ...
My Graph Concepts Playlist : Graph Concepts & Qns - 1 : Graph will...
My GitHub Repo for interview preparation : https://github.com/MAZHARMIK/Intervie...
Subscribe to my channel : @codestorywithmik
Instagram : Instagram: codestorywithmik
Facebook : Facebook: 100090524295846
Twitter : Twitter: CSwithMIK


Approach Summary :
Approach-1 (BFS)
Algorithm:
Uses Breadth-First Search (BFS) to traverse the tree level by level.
Maintains a queue to keep track of nodes at each level.
Uses a boolean flag (even_level) to alternate between even and odd levels.
Checks if values in even levels are strictly increasing and odd levels are strictly decreasing.
Complexity:
Time Complexity: O(n) - where n is the number of nodes in the tree.
Space Complexity: O(n) - in the worst case, when the queue stores all nodes at the maximum level.
Approach-2 (DFS)
Algorithm:
Uses Depth-First Search (DFS) with a recursive approach.
Maintains a vector (levelPrev) to store the previous value at each level.
Checks if values at even levels are strictly increasing and odd levels are strictly decreasing.
Complexity:
Time Complexity: O(n) - where n is the number of nodes in the tree.
Space Complexity: O(n) - uses additional space for the vector levelPrev and recursion stack. The recursion depth is determined by the height of the tree.
These approaches aim to determine if a given binary tree is an Even-Odd Tree based on specific rules about the values in even and odd levels. The BFS approach uses a queue for level-order traversal, while the DFS approach utilizes recursive calls and a vector to track previous values at each level. Both approaches have a time complexity of O(n) but differ in their space complexity.

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

✨ Timelines✨
00:00 - Introduction
02:28 - Approach 1 BFS
11:40 - Coding Approach 1
16:01 - Time & Space
16:18 - Approach 2 DFS
33:43 - Coding Approach 2
38:39- Time & Space

#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
5 ماه پیش در تاریخ 1402/12/09 منتشر شده است.
3,796 بـار بازدید شده
... بیشتر