Delete Leaves With a Given Value - Leetcode 1325 - Python

Delete Leaves With a Given Value - Leetcode 1325 - Python

NeetCodeIO

2 недели назад

5,769 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@dhruvgirigoswami5886
@dhruvgirigoswami5886 - 17.05.2024 05:12

FIRST!

Ответить
@dhruvgirigoswami5886
@dhruvgirigoswami5886 - 17.05.2024 05:12

SECOND!!

Ответить
@nqv497
@nqv497 - 17.05.2024 05:43

THIRD

Ответить
@ChiragMalaviya-so7tw
@ChiragMalaviya-so7tw - 17.05.2024 07:07

Fourth

Ответить
@AnordinaryMan007
@AnordinaryMan007 - 17.05.2024 07:10

fifth

Ответить
@advaitbajaj4241
@advaitbajaj4241 - 17.05.2024 07:16

Sixty Nine!

Ответить
@MP-ny3ep
@MP-ny3ep - 17.05.2024 07:23

Thank you so much for the daily problems.

Ответить
@jayberry6557
@jayberry6557 - 17.05.2024 08:03

Is there any reason why would we use bfs in this case?

Ответить
@user-rv1bx8hx4v
@user-rv1bx8hx4v - 17.05.2024 08:24

Thank you!

Ответить
@krateskim4169
@krateskim4169 - 17.05.2024 08:35

Ayo the thumbnail😂

Ответить
@Ari-pq4db
@Ari-pq4db - 17.05.2024 09:30

Thank You 🍃

Ответить
@ap2s2000
@ap2s2000 - 17.05.2024 10:41

420 everyday

Ответить
@ap2s2000
@ap2s2000 - 17.05.2024 10:41

420 everyday

Ответить
@DeathSugar
@DeathSugar - 17.05.2024 13:32

Dunno why is it medium. Pretty straightforward solution.

Ответить
@pastori2672
@pastori2672 - 17.05.2024 13:59

Ответить
@nirmalgurjar8181
@nirmalgurjar8181 - 17.05.2024 14:00

Accepted Java Iterative Solution using map and stack:

class Solution {
public TreeNode removeLeafNodes(TreeNode root, int target) {

Map<TreeNode, TreeNode> parentMap = new HashMap();
Stack<TreeNode> 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;
}
}

Ответить
@Pegasus02Kr
@Pegasus02Kr - 21.05.2024 06:26

honestly I doubt we will ever have chance to implement tree traversal with iterative approach...

thanks for the explanation

Ответить
@kareni7572
@kareni7572 - 28.05.2024 23:12

Yes, The leaf node check should be done post order (not pre order) -- so that it will reach till parent

Ответить