Skip to content

1110. Delete Nodes And Return Forest #59

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

To solve this problem, we can follow these steps:

  1. Traverse the tree using a helper function.
  2. If a node is marked for deletion, add its children to the result forest if they are not null.
  3. Recursively delete nodes and adjust the tree accordingly.
  4. Return the list of root nodes of the remaining forest.

Let's implement this solution in PHP: 1110. Delete Nodes And Return Forest

<?PHP

class TreeNode {
    public $val = null;
    public $left = null;
    public $right = null;

    public function __construct($val = 0, $left = null, $right = null) {
        $this->val = $val;
        $this->left = $left;
        $this->right = $right;
    }
}

class Solution {

    /**
     * @param TreeNode $root

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
1 participant