LeetCode 1110. Delete Nodes And Return Forest Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
1110. Delete Nodes And Return Forest

Description

Given the root of a binary tree, each node in the tree has a distinct value.

After deleting all nodes with a value in to_delete, we are left with a forest (a disjoint union of trees).

Return the roots of the trees in the remaining forest. You may return the result in any order.

 

Example 1:

Input: root = [1,2,3,4,5,6,7], to_delete = [3,5]
Output: [[1,2,null,4],[6],[7]]

Example 2:

Input: root = [1,2,4,null,3], to_delete = [3]
Output: [[1,2,4]]

 

Constraints:

  • The number of nodes in the given tree is at most 1000.
  • Each node has a distinct value between 1 and 1000.
  • to_delete.length <= 1000
  • to_delete contains distinct values between 1 and 1000.

Solutions

Solution 1: DFS

First, we use a hash table or an array of length 1001, s, to record all nodes that need to be deleted.

Next, we design a function dfs(root) that returns the root of the subtree with root as the root after deleting all nodes that need to be deleted. The execution logic of the function dfs(root) is as follows:

  • If root is null, we return null;
  • Otherwise, we recursively execute dfs(root.left) and dfs(root.right), and assign the return values to root.left and root.right respectively. If root does not need to be deleted, we return root; if root needs to be deleted, we check whether root.left and root.right are null. If they are not null, we add them to the answer array; finally, we return null.

In the main function, we call dfs(root). If the result is not null, it means that the root node does not need to be deleted, and we add the root node to the answer array. Finally, we return the answer array.

The time complexity is O(n), and the space complexity is O(n). Here, n is the number of nodes in the tree.

PythonJavaC++GoTypeScriptJavaScript
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def delNodes( self, root: Optional[TreeNode], to_delete: List[int] ) -> List[TreeNode]: def dfs(root: Optional[TreeNode]) -> Optional[TreeNode]: if root is None: return None root.left, root.right = dfs(root.left), dfs(root.right) if root.val not in s: return root if root.left: ans.append(root.left) if root.right: ans.append(root.right) return None s = set(to_delete) ans = [] if dfs(root): ans.append(root) return ans(code-box)

Solution 2: BFS

TypeScriptJavaScript
/** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } * } */ export function delNodes(root: T, to_delete: number[]): Array<T> { if (!root) return []; const del = new Set(to_delete); const res: T[] = []; let q: TreeNode[] = [root]; while (q.length) { const qNext: TreeNode[] = []; for (const node of q) { if (node.left) { qNext.push(node.left); if (del.has(node.left.val)) { node.left = null; } } if (node.right) { qNext.push(node.right); if (del.has(node.right.val)) { node.right = null; } } if (del.has(node.val)) { if (node.left) res.push(node.left); if (node.right) res.push(node.right); } } q = qNext; } if (!del.has(root.val)) res.push(root); return res; } type T = TreeNode | null;(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !