Even Odd Tree | DFS | BFS | Leetcode 1609
Whatsapp Community Link : https://www.whatsapp.com/channel/0029Va6kVSjICVfiVdsHgi1A
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/Interview_DS_Algo/blob/master/Tree/Even%20Odd%20Tree.cpp
Leetcode Link : https://leetcode.com/problems/even-odd-tree/
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
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