Delete Leaves With a Given Value - Leetcode 1325 - Python

  Рет қаралды 6,805

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 23
@kareni7572
@kareni7572 5 ай бұрын
Yes, The leaf node check should be done post order (not pre order) -- so that it will reach till parent
@krateskim4169
@krateskim4169 6 ай бұрын
Ayo the thumbnail😂
@Pegasus02Kr
@Pegasus02Kr 6 ай бұрын
honestly I doubt we will ever have chance to implement tree traversal with iterative approach... thanks for the explanation
@MP-ny3ep
@MP-ny3ep 6 ай бұрын
Thank you so much for the daily problems.
@Ari-pq4db
@Ari-pq4db 6 ай бұрын
Thank You 🍃
@DeathSugar
@DeathSugar 6 ай бұрын
Dunno why is it medium. Pretty straightforward solution.
@chrischika7026
@chrischika7026 6 ай бұрын
look at the iterative.
@DeathSugar
@DeathSugar 6 ай бұрын
@@chrischika7026 uglier and complicated, more memory footprint and not really outperforms recursive one. Would work on the languages which has no recursion, but they aren't on the language list in leetcode anyway, so why bother?
@NursultanBegaliev
@NursultanBegaliev 6 ай бұрын
Thank you!
@jayberry6557
@jayberry6557 6 ай бұрын
Is there any reason why would we use bfs in this case?
@staywithmeforever
@staywithmeforever 6 ай бұрын
We can use dfs also no problem.
@staywithmeforever
@staywithmeforever 6 ай бұрын
But if you wanna use bfs u need parent pointers that will be complicated.
@staywithmeforever
@staywithmeforever 6 ай бұрын
U have to make a while look and keep on deleting the parents while target!=root.val. so this makes O(n2)
@nirmalgurjar8181
@nirmalgurjar8181 6 ай бұрын
Accepted Java Iterative Solution using map and stack: class Solution { public TreeNode removeLeafNodes(TreeNode root, int target) { Map parentMap = new HashMap(); Stack stk = new Stack(); stk.push(root); while (!stk.isEmpty()) { TreeNode node = stk.pop(); if (node.right == null && node.left == null && node.val == target) { // delete this node TreeNode parent = parentMap.get(node); if (parent != null && parent.right == node) { // update parent right pointer to null parentMap.remove(node); parent.right = null; } if (parent != null && parent.left == node) { // update parent left pointer to null parentMap.remove(node); parent.left = null; } } else { boolean added = false; if (node.right != null && !parentMap.containsKey(node.right)) { added = true; stk.push(node); // add back current node to stack stk.push(node.right); parentMap.put(node.right, node); } if (node.left != null && !parentMap.containsKey(node.left)) { if (!added) { stk.push(node); // if not added ealier then add back } stk.push(node.left); parentMap.put(node.left, node); } } } //if root only is pending and having value == target if (root.left == null && root.right == null && root.val == target) { return null; } return root; } }
@advaitbajaj4241
@advaitbajaj4241 6 ай бұрын
Sixty Nine!
@ChiragMalaviya-so7tw
@ChiragMalaviya-so7tw 6 ай бұрын
Fourth
@AnordinaryMan007
@AnordinaryMan007 6 ай бұрын
fifth
@ap2s2000
@ap2s2000 6 ай бұрын
420 everyday
@nqv497
@nqv497 6 ай бұрын
THIRD
@pastori2672
@pastori2672 6 ай бұрын
@dhruvgirigoswami5886
@dhruvgirigoswami5886 6 ай бұрын
FIRST!
@dhruvgirigoswami5886
@dhruvgirigoswami5886 6 ай бұрын
SECOND!!
@ap2s2000
@ap2s2000 6 ай бұрын
420 everyday
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 148 М.
Freedom Trail - Leetcode 514 - Python
25:18
NeetCodeIO
Рет қаралды 14 М.
How Many Balloons To Make A Store Fly?
00:22
MrBeast
Рет қаралды 96 МЛН
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 8 МЛН
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 688 М.
Maximum Matrix Sum - Leetcode 1975 - Python
16:07
NeetCodeIO
Рет қаралды 1,8 М.
Coding Unbreakable Encryption in C | One-Time Pad
17:42
HirschDaniel
Рет қаралды 4,6 М.
Distribute Coins in Binary Tree - Leetcode 979 - Python
17:41
NeetCodeIO
Рет қаралды 17 М.
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 852 М.
why are switch statements so HECKIN fast?
11:03
Low Level
Рет қаралды 427 М.
Score After Flipping Matrix - Leetcode 861 - Python
22:30
NeetCodeIO
Рет қаралды 10 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 197 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 438 М.