Vertical order traversal of a binary tree | Leetcode

This video explains how to find the vertical order traversal of a binary tree which is from Leetcode 987.This problem is slightly different from the easier version where we are just required to find vertical order traversal based on Y-axis only.In this problem, we are required to find vertical order traversal of a binary tree based on X and Y axis.We can solve it using DFS as well as BFS.I have explained in detail the DFS approach but simple DFS/Recursion won't work which i have shown using an example.I have the data structure which will be used to solve it with reason.We can use Map of Map of Set in order to maintain the constraints of the problem statement.I have explained the entire algorithm to find vertical level order traversal using example.At the end of the video, i have explained the CODE walk through.CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
========================================================================
Join this channel to get access to perks:
/ @techdose4u
INSTAGRAM : / surya.pratap.k
SUPPORT OUR WORK: / techdose
LinkedIn: / surya-pratap-kahar-47b...
WEBSITE: techdose.co.in/
TELEGRAM Channel LINK: t.me/codewithTECHDOSE
TELEGRAM Group LINK: t.me/joinchat/SRVOIxWR4sRIVv5...
=======================================================================
CODE LINK: gist.github.com/SuryaPratapK/...

Пікірлер: 121

  • @pranayreddy6337
    @pranayreddy63373 жыл бұрын

    The solution is really good, but I would like to point out that using set doesn't handle duplicate values. So to pass all the cases use multiset!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    👍

  • @neversettlejay

    @neversettlejay

    2 жыл бұрын

    wait set doesn't handle duplicate values means!!!!? Set is meant to take just unique values right?

  • @dineshsaini6051

    @dineshsaini6051

    2 жыл бұрын

    @@neversettlejay yes, hence you'll end up missing duplicate values.

  • @pranavsharma7479

    @pranavsharma7479

    2 жыл бұрын

    use multiset

  • @commentator2407

    @commentator2407

    Жыл бұрын

    @@neversettlejay ya

  • @dsacppracticequestion1123
    @dsacppracticequestion11232 жыл бұрын

    Your teaching style is rock solid thanks for this wonderful explanation of the topic I stumbled upon.

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Thanks 👍🏼

  • @code_life4835
    @code_life48353 жыл бұрын

    I was getting runtime Error, but you solved my doubt. Thanks a lot for your Awesome Explanation!!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @ankitkumar-ih8qo
    @ankitkumar-ih8qo3 жыл бұрын

    Your intuition to explain is so good , and I have a doubt on for loop.... Would you please explain how the for loop work.? It really helpful to me

  • @vbalas9214
    @vbalas92142 жыл бұрын

    what does iterating over m1.second and m2.second actually mean? Is m2.second having the values of the tree in the ascending order? And we are pushing a vector() into the ans 2d vector, is it just to create some space in the answer vector?

  • @VenkatIyer
    @VenkatIyer2 жыл бұрын

    Inside the first for loop, the first statement of pushing the vector() means are we pushing the entire vector?? I am confused with the representation.

  • @raihanmdsiqbal9097
    @raihanmdsiqbal90973 жыл бұрын

    Please keep this series on going and free of cost. Please

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    😳

  • @mayankgupta8715
    @mayankgupta87154 жыл бұрын

    Ye tech ki dose bahut imp h.Ye sab coders ko leni chaiye.....

  • @tusharbhatia9930
    @tusharbhatia99302 жыл бұрын

    Can someone tell why "ans.push_back(vector())" is used in 1st approach);

  • @sixth_sense_working
    @sixth_sense_working2 жыл бұрын

    can you let me know why we have inserting value on basis of col * row and not in row * col.

  • @mrmrigank7154
    @mrmrigank71543 жыл бұрын

    thank you sir , your lectures help very much I just watched little of it and understood what to do next, thank you

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Nice :)

  • @amitavamozumder73
    @amitavamozumder733 жыл бұрын

    i don't think we have similar DS in nodeJS, map and set both follow insertion order here, so we manually need to sort the keys/values.

  • @ankitkumar-ih8qo
    @ankitkumar-ih8qo2 жыл бұрын

    great explanation, ans.push_back(vector()) , I dont understand what is these and why we use "vector()" ? Would you please explain me

  • @yitingg7942
    @yitingg79423 жыл бұрын

    Solved it one try within 20 mins using treemap of treemap of priorityQueue. I must say I learned well from you lol. Thank you so much legend!!!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome

  • @kunalsoni7681
    @kunalsoni76814 жыл бұрын

    Seriously very amazing solutions of this problem you provide 😊.. thanking you so much sir

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @adishijha8365
    @adishijha83654 жыл бұрын

    Thank you so much! I was stuck at this question.. you made it clear.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @okeyD90232
    @okeyD902322 жыл бұрын

    wonderful explanation, solved this leetcode problem in one go just by following your explanation .

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Great ❤️

  • @pranavsharma7479
    @pranavsharma74792 жыл бұрын

    best explaination for this problem and dfs is easier than bfs in this tbh

  • @CodeBrownie739
    @CodeBrownie73911 ай бұрын

    finally understood the problem lots of the Thank you!!❤❤

  • @sudinjana4402
    @sudinjana44022 жыл бұрын

    but if we have duplicates elements in same row and column and set will not work it would be better to use vector instead of set

  • @gauravshukla6675
    @gauravshukla66753 жыл бұрын

    what is the time complexity of this code?

  • @princeakhil208
    @princeakhil2084 жыл бұрын

    The content of this channel is growing exponentially surely this will become a repository to crack faang interviews thanks for your effort tech dose

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks for your appreciation. I hope it helps every student preparing for programming interviews.

  • @easycodingwithsumit
    @easycodingwithsumit2 жыл бұрын

    Too good explaination.This video is really good and all contents are easily understable .

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Thanks 😊

  • @dollyakshaya7535
    @dollyakshaya7535 Жыл бұрын

    bhaiya explanation is awsm

  • @varunnadipudi2257
    @varunnadipudi22574 жыл бұрын

    Nicely explained🙌..keep up with this awesome work.👍

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks

  • @Apoorvpandey
    @Apoorvpandey2 жыл бұрын

    For duplicate values in Python simply use List and make sorting arrangements accordingly (DFS approach)

  • @motivationdaily37
    @motivationdaily37 Жыл бұрын

    Which will run faster BFS or DFS?

  • @AdityaPratapSingh-ss8wg
    @AdityaPratapSingh-ss8wg3 жыл бұрын

    How to print top view of bi art tree in o(n)

  • @mayurganatra112
    @mayurganatra1124 жыл бұрын

    I was not able to solve the problem due to 1st constraint, thank you for the solution.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @harshraj57
    @harshraj573 жыл бұрын

    can we solve it recursively??

  • @infidarkstudios2066
    @infidarkstudios20664 жыл бұрын

    Awesome solution dude. Hats off to you.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @bitrish34
    @bitrish343 жыл бұрын

    What should be the space complexity?

  • @ajitshiva9193
    @ajitshiva91934 жыл бұрын

    Brilliant approach.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @santoshreddy3
    @santoshreddy33 жыл бұрын

    Nice explanation. One finding though, Set will remove duplicates. For example if we have same values for given (x, y) coordinates, we will endup missing one value in output. Am I missing something?

  • @ratankumar1399

    @ratankumar1399

    2 жыл бұрын

    But it is assumed that there is no duplicates in Tree,if there is duplicates then go for multiset

  • @harshbokan6874

    @harshbokan6874

    2 жыл бұрын

    Use multiset

  • @dhanashreegodase4445
    @dhanashreegodase44452 жыл бұрын

    Huge thanks💜

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Welcome 😀

  • @milenitrivedi7561
    @milenitrivedi75612 жыл бұрын

    Instead of using set using multiset will solve the problem of having duplicates in same row and col.

  • @priyankanagaraju7590
    @priyankanagaraju75903 жыл бұрын

    Really good explanation !

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @sunilthind3257
    @sunilthind32573 жыл бұрын

    Hello, Could you please help how to solve this question using JavaScript? Because map and set are little different in JS.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    I will ask people to share their code in comment in different languages. It will be much more helpful.

  • @JohnWick-kh7ow
    @JohnWick-kh7ow2 жыл бұрын

    Striver said that the time complexity is O(N*logN). You are saying that time complexity is O(N*logN*logN*logN). Which one is right?

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    In the Map Map Set, if you use Unordered_map then time will be NlogN in total. If you use Map then NlogNlogNLogN. 2 extra logs for ordered map key search. So. It depends on your choice of DS.

  • @JohnWick-kh7ow

    @JohnWick-kh7ow

    2 жыл бұрын

    @@techdose4u I asked him he agreed that time complexity will be O(N*LogN*LogN*LogN). He told the time complexity of java code which is O(N*logN)

  • @SHASHANKRUSTAGII
    @SHASHANKRUSTAGII2 жыл бұрын

    Better than striver! But you could have used unordered map and mutilset instead of map and set.

  • @ShaliniNegi24
    @ShaliniNegi243 жыл бұрын

    Nice explanation!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @sakthim7160
    @sakthim71604 жыл бұрын

    Will you able to make videos on binary lifting? Which will be very helpful to solve problems like lowest common ancestor of given two nodes and kth ancestors of a given node in a tree.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Its difficult to accomodate with graph videos as many of them are pending as well. So, I will try after graph.

  • @sakthim7160

    @sakthim7160

    4 жыл бұрын

    @@techdose4u thank you!

  • @abhisheksuryavanshi9675
    @abhisheksuryavanshi96753 жыл бұрын

    Not working for test case-> [1,2,3,4,5,6,null,null,7,8,null,null,9,null,10,null,11,10],hence instead of set I took vector in map

  • @sagnikroy4018
    @sagnikroy40183 жыл бұрын

    Thank you so much

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @prakharprakash4227
    @prakharprakash42272 жыл бұрын

    Excellent explanation sir

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Thanks 😃

  • @AbhishekKumar-zs8zt
    @AbhishekKumar-zs8zt3 жыл бұрын

    awesome explanation

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @vijayjayaram606
    @vijayjayaram6063 жыл бұрын

    Thay asked the same today in my interview Lucky me I watched this video after the interview Thank you youtube Ur recommendation algo sucks!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    How does youtube know that you want to see this 😂 Google knows about your interview too!!

  • @vijayjayaram606

    @vijayjayaram606

    3 жыл бұрын

    @@techdose4u scary. My meeting invite 😱

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    @@vijayjayaram606 Haha :P

  • @ashwinvarma9349
    @ashwinvarma93493 жыл бұрын

    That was epic explanation!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @AINeptune
    @AINeptune2 жыл бұрын

    how were we supposed to figure this out on ur own without looking at the solution?

  • @hemanthram4369
    @hemanthram43693 жыл бұрын

    good problem

  • @rohitkumaram
    @rohitkumaram2 жыл бұрын

    where you work Google or Meta?

  • @praveenprakash6336
    @praveenprakash63364 жыл бұрын

    Bro your explanation is truly good. Bro could you please make a video for 1st year students in order to get into product based companies.

  • @praveenprakash6336

    @praveenprakash6336

    4 жыл бұрын

    For students from tier 3 college.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    I will try to make it.

  • @praveenprakash6336

    @praveenprakash6336

    4 жыл бұрын

    @@techdose4u we will be waiting ..

  • @depression_plusplus6120

    @depression_plusplus6120

    3 жыл бұрын

    @@praveenprakash6336 forget bro...we NITS IITANS deserve them..you go for TCS🤣

  • @praveenprakash6336

    @praveenprakash6336

    3 жыл бұрын

    @@depression_plusplus6120 Don't judge a guy by his college okay.....

  • @deepjyotkaurbindra
    @deepjyotkaurbindra3 жыл бұрын

    BRILLIANT

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    😀

  • @noobninja4882
    @noobninja48824 жыл бұрын

    Thanks sir

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @aneeshreddy1534
    @aneeshreddy15343 жыл бұрын

    what is the difficulty level of this one

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Medium I think

  • @bhushanb8339
    @bhushanb83393 жыл бұрын

    Code link is not reachable..

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    I will check

  • @vijayjayaram606
    @vijayjayaram6063 жыл бұрын

    Approach = 🤘

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    🤘🏼

  • @humble.660
    @humble.660 Жыл бұрын

    Code in Python for DFS(It becomes helpful with sorted function): class Solution: def helper(self, placement,level, root, dic): if(not root): return dic[placement].append((level, root.val)) self.helper(placement-1, level+1, root.left, dic) self.helper(placement+1, level+1, root.right, dic) def verticalTraversal(self, root: TreeNode) -> List[List[int]]: dic = defaultdict(list) self.helper(0,0, root, dic) result = [] for i in sorted(dic.keys()): temp = [] for j in sorted(dic[i]): temp.append(j[1]) result.append(temp) return result

  • @CodeSuccessChronicle
    @CodeSuccessChronicle3 жыл бұрын

    Saviour!!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    :)

  • @md.giashuddin3083
    @md.giashuddin3083 Жыл бұрын

    This problem can be solved using HashMap & PriorityQueue in Java. `class Solution { public List verticalTraversal(TreeNode root) { Map map = new HashMap(); int[] width = {0, 0}; verticalTraversal(root, 0, 0, map, width); List result = new ArrayList(); for (int i = width[0]; i

  • @neversettlejay
    @neversettlejay2 жыл бұрын

    TreeMap

  • @sase1017
    @sase10173 жыл бұрын

    Hard to understand

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Please watch it again

  • @sase1017

    @sase1017

    3 жыл бұрын

    @@techdose4u Thank you so much, I understand pretty well and be able to code out by myself now, your visualization and explanation helped tremendously

  • @AsifKhan-fn2cy
    @AsifKhan-fn2cy3 жыл бұрын

    Nice explanation !

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)