Subtree of Another Tree - Leetcode 572 - Trees (Python)

  Рет қаралды 3,901

Greg Hogg

Greg Hogg

Күн бұрын

Пікірлер: 14
@GregHogg
@GregHogg 4 ай бұрын
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@christianjt7018
@christianjt7018 4 ай бұрын
Great solution! I suggest a minor optimization: check the subtree only if the node and the head of the subroot have the same value.
@franciscoserra8455
@franciscoserra8455 Ай бұрын
I tested that out and the difference was minus 4 ms on average, so that's really really really a minor optimization that I would disregard in order to prioritize code readability :)
@JoeTan-nq4fq
@JoeTan-nq4fq 6 күн бұрын
Can further optimise by using stack (instead of recursion). stack = [root] while stack: node = stack.pop() # Initiate comparison if root nodes are the same if node.val == subRoot.val and isSameTree(node, subRoot): return True # Append child nodes if exist if node.right: stack.append(node.right) if node.left: stack.append(node.left) return False
@LemesaElias
@LemesaElias 3 ай бұрын
I like how you approach problems and how you don't use some weird built-in functions. Everything is clear and understandable.
@updownftw
@updownftw 17 күн бұрын
Hi Greg, what App are you using for Drawing ?
@jacky-L-327
@jacky-L-327 7 ай бұрын
what’s the m + n solution
@GregHogg
@GregHogg 7 ай бұрын
You can read about it in the leetcode editorial section, it's kinda weird
@aly7401
@aly7401 6 ай бұрын
Thank you for your tree-based problems video 🎉
@saideep7510
@saideep7510 Ай бұрын
@@GregHogg But would this (m*n) solution be good enough for faang interview or would we be expected to know (m+n) solution as well?
@youngbaron1504
@youngbaron1504 Ай бұрын
@@saideep7510i think m+n is very advanced
@piglovesasy
@piglovesasy 3 ай бұрын
this does not feel like an easy problem, more like a medium
@praneetkomandur6576
@praneetkomandur6576 5 ай бұрын
do we need to really know about the time complexity of every leetcode problem we solve
@GregHogg
@GregHogg 5 ай бұрын
Yes
Pacific Atlantic Water Flow - Leetcode 417 - Graphs (Python)
17:10
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН
When you have a very capricious child 😂😘👍
00:16
Like Asiya
Рет қаралды 8 МЛН
Smart Sigma Kid #funny #sigma
00:33
CRAZY GREAPA
Рет қаралды 39 МЛН
1% vs 100% #beatbox #tiktok
01:10
BeatboxJCOP
Рет қаралды 60 МЛН
Subtree of Another Tree - Leetcode 572 - Python
14:15
NeetCode
Рет қаралды 174 М.
Diameter of Binary Tree - Leetcode 543 - Trees (Python)
11:16
How to solve (almost) any binary tree coding problem
4:20
Inside code
Рет қаралды 194 М.
Coin Change - Leetcode 322 - Dynamic Programming (Python)
15:27
The Truth About Learning Python in 2024
9:38
Internet Made Coder
Рет қаралды 222 М.
Rotting Oranges - Leetcode 994 - Graphs (Python)
16:09
Greg Hogg
Рет қаралды 3,8 М.
Diameter of a Binary Tree - Leetcode 543 - Python
15:34
NeetCode
Рет қаралды 238 М.
I've read 57 Books on AI and Data Science - these are the best (for 2025)
9:29
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН