LeetCode Invert Binary Tree Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

Eight years as a software engineer, I finally know how to invert a binary tree (and what it even means).

usptact
Автор

Wow, I can't believe I thought this was way more complex than it actually was. Thank you.

zacisrael
Автор

“Do you know what the scariest thing in the world is?”

Ohmriginal
Автор

The explanation part for the code wasn’t the best haha but I figured it out. Not a bad video but try teaching things like we are stupid.

pooblock
Автор

This is deceptively simple. I can't believe I've been avoiding this particular problem for so long. I'm off to find similar problems so I can practise!

uni
Автор

It was mentioned in the leetcode solution BFS approach would save stack overhead to be more scalable

兴宣大院君-hs
Автор

Thanks for the explanation! I’m a little confused why we don’t need to save the original right child value. I was thinking when we say root.right=left that this would make it so that both children hold the value of the left child. So on the following line when we say root.left=right we’re essentially setting left to left. Thanks again!

ryanmichaud
Автор

Same as reflecting it ? Or mirroring ?

AviPars
Автор

But one must first understand recursion in order to understand recursion…

alexander
Автор

Just wondering, how does it go back up to 2 if it's DFS? It seems like it ends at 1 so I'm not sure how it travels back up. Please help😢

yu
Автор

Holy shit, thanks Nick! Now I know how to invert a binary tree :D

OverLordOfDardWorld
Автор

I think following could work as well, if tree is balanced then use array to store nodes, then reverse 2^k elements, so 4 -> 4, 27 -> 72 and 1369 -> 9631 and so on.
4 - 27 - 1369
4 - 72 - 9631

edino
Автор

The explanation made sense. Only 150 IQ could comr up with problem without looking at Algorithm ahead of time

catharsis
Автор

Thank you Nick for such an explanation

ThabiraGamingShorts
Автор

Your code is fucking as intuitive as your logic! Love it! I Love you Nick!

lucassmith
Автор

This is slightly simpler. Top down invert.
private static void invert(TreeNode current) {
if (current == null) {
return;
}
TreeNode left = current.left;
current.left = current.right;
current.right = left;
invert(current.left);
invert(current.right);
}

oscaropdyou
Автор

This is my bunk ass solution(first thing that I came up with)

public TreeNode invertTree(TreeNode root) {
if(root == null || (root.left == null && root.right == null))
return root;

// swap
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;

// move to next nodes
invertTree(root.left);
invertTree(root.right);

return root;

}

ipodtouch
Автор

Would you like to talk about the Inception thing happened at the beginning of the video ?

vijaykumarreddyalavala
Автор

yeah ya know itll just ya know go back up ya know set left ya know to right and ya know ill explain that later ya know and itll hit this statement ya know ok thanks guys

Dmetal
Автор

I hate the syntax of how to deal with these trees, I'm so used to things like arrays and strings and iterating through them using for loops and while loops that this feels completely brand new

IbytheGOAT