LeetCode Binary Tree Inorder Traversal Solution Explained - Java

  Рет қаралды 91,732

Nick White

Nick White

Күн бұрын

Пікірлер: 53
@ConwellConwell
@ConwellConwell Жыл бұрын
Nick helped code 911 and grinder
@sivaganesh4489
@sivaganesh4489 4 жыл бұрын
That clap at the beginning makes me alert and my mind says please be concentrate you gonna be learn something new
@osxs333__7
@osxs333__7 4 жыл бұрын
makes me feel like a dog when you put it like that
@mudassirshahzadkk
@mudassirshahzadkk 4 жыл бұрын
You can use recursion for this. It's much easier. Your call stack may increase but it's still gonna be O(n) for both time and space.
@CoolnesXcore
@CoolnesXcore 4 жыл бұрын
True but the question asks you to do it iteratively.
@mudassirshahzadkk
@mudassirshahzadkk 4 жыл бұрын
@@CoolnesXcore You're right. In an interview, you could explain that recursive solution maybe better because this is exactly what they look for. You ability to choose a better solution.
@amanmalhotra2026
@amanmalhotra2026 3 жыл бұрын
class Solution { List list = new ArrayList(); public List inorderTraversal(TreeNode root) { if(root == null){ return list; } inorderTraversal(root.left); list.add(root.val); inorderTraversal(root.right); return list; } }
@RishabhMishra-uw9yb
@RishabhMishra-uw9yb 3 жыл бұрын
amazing man thanks for the code i was looking for this kind of code
@TheOkayUek
@TheOkayUek 3 жыл бұрын
They asked without recursion
@harshsinha3221
@harshsinha3221 2 жыл бұрын
Everyone is gangsta, until you have to traverse a binary tree iteratively.
@harshitsaxena3064
@harshitsaxena3064 3 жыл бұрын
Great Work Nick ! Whenever I saw that you have made a video on any leetcode question I become very happy because I know I will connect with your logic and your thought process.
@mostinho7
@mostinho7 2 жыл бұрын
TODO: take note of this, doing inorder traversal without using recursion
@evanpoole7829
@evanpoole7829 Жыл бұрын
around 0:27 is a great explanation!!
@samyakjain6855
@samyakjain6855 3 жыл бұрын
Coming back after a year, still impressed...keep up the brilliant work Nick!
@giorgi23
@giorgi23 4 жыл бұрын
Nick you explain well, your implementations are brilliant. don't torture yourself!!! The only thing you need is a white board stuff(maybe e-version) to show more details. Especially for beginners it's sometimes hard to read only from code.
@ericytff7388
@ericytff7388 10 ай бұрын
its been 6 years, too good !
@mohammedghabyen721
@mohammedghabyen721 2 жыл бұрын
public class Solution { public IList InorderTraversal(TreeNode root) { IList res = new List(); addData(root,res); return res; } private void addData(TreeNode node,IList res){ if(node==null) return; addData(node.left,res); res.Add(node.val); addData(node.right,res); } }
@paulsnehasish5830
@paulsnehasish5830 3 жыл бұрын
so it was medium problem way back in 2018 and now its under easy category :D
@Gideon_Judges6
@Gideon_Judges6 5 жыл бұрын
I wonder if the recursive version would give you a better performance rating in java bytecode.
@sannge2631
@sannge2631 4 жыл бұрын
the question asks you to solve the problem iteratively.
@andreykovalov438
@andreykovalov438 2 жыл бұрын
i call it "thanks for nothing explanation"
@mohamedsalman6934
@mohamedsalman6934 3 жыл бұрын
what is the use of storing the root to current? why cannot we use the root for traversing through the tree?
@user-18873ty
@user-18873ty 3 жыл бұрын
I just have one question, should the one be smart so he can solve those problems? how can I solve new problems by myself if every problem has a new idea
@_abhishekmj_
@_abhishekmj_ 3 жыл бұрын
I m not expert, but I can say it's like solving math problems.. So, you learn few patterns and then apply them for the problems to solve them.. Some will be tough like dynamic programming.. So, need to spend time in learning and keep solving!
@_abhishekmj_
@_abhishekmj_ 3 жыл бұрын
Once you keep doing, you get used to - all skills look difficult before learning!
@hanscesa5678
@hanscesa5678 3 жыл бұрын
at 6:47 I died hahahaha the explanation is confusing if u're hearing it for the first time, but hearing it again will eventually make sense xD
@sproutboot
@sproutboot 2 жыл бұрын
2022, now this problem is moved to 'easy'..?
@curtism7302
@curtism7302 4 жыл бұрын
This is from about a year and half ago. Nick, my main man, I bet you could redo this video with a cleaner and little clearer explanation by now. That being said, I did understand after watching this a few times. Also(if you even read these) , Did you get the job at Amazon?
@michaelyao9389
@michaelyao9389 2 жыл бұрын
Do we need Morris way?
@henry6165
@henry6165 2 жыл бұрын
Can someone explain why time complexity is o(n) even if we are using two iterations?
@marcoscarlomagno3065
@marcoscarlomagno3065 2 жыл бұрын
Because the complexity does not necessarily depend on the amount of loops, it depends on the time it takes the program to return the result as the input increases, when you add 1 new node to the input the program just requires a new cicle in the big loop. This is linear complexity O(n) = m (array size n, m cicles) O(n+1) = m + 1. (array size n+1, m+1 cicles) O(n+2) = m + 2. (array size n+2, m+2 cicles) As you can see the time increases in a linear relationship with the input.
@abhinavdadhich2311
@abhinavdadhich2311 4 жыл бұрын
i cant understand plz help..how it going to root first instead of right node after executing 22 line.
@frogery
@frogery 4 жыл бұрын
because you pushed all the left nodes on the stack first -> [1,2,4] so when you pop 4 and go to the next iteration, the root node (2) will be at the top of the stack and get popped, and then 2's right node is added to the stack.
@dengzhonghan5125
@dengzhonghan5125 4 жыл бұрын
I had the same questions after watching this video. I just work it out. I hope my explanation would help. When we reach the very left which is 4, then curr = 4 which is the very top of the stack. After that, we save 4 to res. And then, curr.right which is 4's right is None. We will go to the next iteration. Because curr is None, we will not go into the second while loop. The next element in the stack will pop out which is 2. We save 2 to res. Then we set curr = 2's right which is 5. This time curr is not None, we will go into the second while loop so we save 5 in stack.
@sofiayz7472
@sofiayz7472 4 жыл бұрын
@@dengzhonghan5125 So when the current value is 5, current.left is null and that's why 5 get popped out?
@harshitsaxena3064
@harshitsaxena3064 3 жыл бұрын
@@dengzhonghan5125 Thankyou very much deng Your explanation is really good and thanks for clearing my doubt.
@lifeofme3172
@lifeofme3172 4 жыл бұрын
Thanks but needed a bit clearer explanation.
@benvan3721
@benvan3721 2 жыл бұрын
Thank you. but reading the code is not what I expect. you're having hard time to explain what the code does.
@mrproteinz
@mrproteinz 5 жыл бұрын
why not apply a recursion? Is this more efficient?
@vishalscit
@vishalscit 5 жыл бұрын
Godfred Oppong the follow up in the problem asks to do it iteratively.
@harshsinha3221
@harshsinha3221 2 жыл бұрын
Recursion, can eventually lead to stack over low error
@nehurane
@nehurane 3 жыл бұрын
Nick great stuff you doing. Explanation does make sense but yeah don’t be nervous as it overwhelms. But keep doing leetcode series
@intermezzoprincess5575
@intermezzoprincess5575 Жыл бұрын
Interestingly, this problem is categorized as "easy" on Leetcode now haha.
@jetdan521
@jetdan521 Жыл бұрын
It's helpful and thanks .
@robinmahato
@robinmahato 5 жыл бұрын
nice explanations
@learning2cube759
@learning2cube759 4 жыл бұрын
It was good man
@abhinavghosh725
@abhinavghosh725 4 жыл бұрын
i like how he says hopefully it will work when the submission page literally shows he submitted it moments ago also!
@RyeCA
@RyeCA 3 жыл бұрын
thx man, this helped!
@chenchangyuan6551
@chenchangyuan6551 4 жыл бұрын
Thanks
@mdraihatuljannatsaimon520
@mdraihatuljannatsaimon520 3 жыл бұрын
Can anyone show me how to implement test cases with main method? I feel ashamed that I can't figure it out smh
@andy0401ify
@andy0401ify 2 жыл бұрын
lol. sooooo cute
@pete1288
@pete1288 2 жыл бұрын
This is all over the place, don't seem to have a grasp on it.
@mohan-ri6ze
@mohan-ri6ze 4 жыл бұрын
Who are all came after Anime part 2(joma tech)
I Got Rejected (again)
9:43
Nick White
Рет қаралды 205 М.
Симбу закрыли дома?! 🔒 #симба #симбочка #арти
00:41
Симбочка Пимпочка
Рет қаралды 5 МЛН
Как Я Брата ОБМАНУЛ (смешное видео, прикол, юмор, поржать)
00:59
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 30 МЛН
LeetCode Search A 2D Matrix Solution Explained - Java
11:47
Nick White
Рет қаралды 39 М.
LeetCode 5.  Longest Palindromic Substring (Algorithm Explained)
14:40
LeetCode Binary Tree Right Side View Explained - Java
6:28
Nick White
Рет қаралды 16 М.
LeetCode Palindrome Linked List Solution Explained - Java
9:35
Nick White
Рет қаралды 112 М.
LeetCode Maximum Width of a Binary Tree Explained - Java
12:21
Nick White
Рет қаралды 35 М.
LeetCode Two Sum Solution Explained - Java
10:31
Nick White
Рет қаралды 237 М.
Симбу закрыли дома?! 🔒 #симба #симбочка #арти
00:41
Симбочка Пимпочка
Рет қаралды 5 МЛН