# LeetCode-Python-1325.Delete leaf nodes of a given value (DFS+Topological Sort)

Posted by spoons84 on Thu, 23 Jan 2020 02:07:25 +0100

Give you a binary tree with root and an integer target. Please delete all leaf nodes with target value.

Note that once a leaf node whose value is target is deleted, its parent node may become a leaf node; if the value of the new leaf node happens to be target, then this node should also be deleted.

That is, you need to repeat this process until you can't continue deleting it.

Example 1:

Input: root = [1,2,3,2,null,2,4], target = 2
Output: [1,null,3,null,4]
Explanation:
In the figure on the left above, the green nodes are leaf nodes, and their values are the same as the target (the same as 2), and they are deleted to get the middle figure.
A new node becomes a leaf node and has the same value as the target, so it will be deleted again to get the rightmost graph.
Example 2:

Input: root = [1,3,3,3,2], target = 3
Output: [1,3,null,null,2]
Example 3:

Input: root = [1,2,null,2,null,2], target = 2
Output: [1]
Interpretation: A green leaf node (value 2) is deleted at each step.
Example 4:

Input: root = [1,1,1], target = 1
Output: []
Example 5:

Input: root = [1,2,3], target = 1
Output: [1,2,3]

Tips:

1 <= target <= 1000
Each tree has a maximum of 3000 nodes.
The range of values for each node is [1,1000].

Source: LeetCode

The first way of thinking is:

The tree problem first considers whether it can be recursively solved by DFS.

For a tree, if the left subtree is already processed, the right subtree is also processed.

Then the rest of the root nodes are simple to solve:

If both left and right children are empty and the root node has a value of target, return None.

Otherwise, return the root node.

Time Complexity: O(N)

Spatial Complexity: O(N)

```class Solution(object):
def removeLeafNodes(self, root, target):
"""
:type root: TreeNode
:type target: int
:rtype: TreeNode
"""
if not root:
return root
root.left = self.removeLeafNodes(root.left, target)
root.right = self.removeLeafNodes(root.right, target)
if not root.left and not root.right and root.val == target:
return None
return root  ```

Second way of thinking:

Similar LeetCode-Python-366.Finding leaf nodes of a complete binary tree But with an additional target condition,

So you can also use topological ordering to solve problems.

Time Complexity: O(N)

Spatial Complexity: O(N)

```class Solution(object):
def removeLeafNodes(self, root, target):
"""
:type root: TreeNode
:type target: int
:rtype: TreeNode
"""
from collections import defaultdict, deque
if not root:
return root
outdegree = dict()
parent = dict()

# Sequence traversal preprocessing
queue = deque([root])
while queue:
for _ in range(len(queue)):
cur = queue.popleft()
outdegree[cur] = 0
if cur.left:
outdegree[cur] += 1
parent[cur.left] = cur
queue.append(cur.left)
if cur.right:
outdegree[cur] += 1
parent[cur.right] = cur
queue.append(cur.right)

queue = deque([])
for key, val in outdegree.items():
if not val and key.val == target:
queue.append(key)

while queue:
cur = queue.popleft()
if cur not in parent:
return None
par = parent[cur]
if par.left and par.left == cur:
par.left = None
outdegree[par] -= 1
if par.right and par.right == cur:
par.right = None
outdegree[par] -= 1

if outdegree[par] == 0 and par.val == target:
queue.append(par)

return root```

687 original articles were published. 88% were praised. 150,000 visits+

Topics: network REST Python