题目地址:https://leetcode.com/problems/delete-nodes-and-return-forest/

题目描述

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]]

Constraints:

1、 Thenumberofnodesinthegiventreeisatmost1000.;
2、 Eachnodehasadistinctvaluebetween1and1000.;
3、 to_delete.length<=1000;
4、 to_deletecontainsdistinctvaluesbetween1and1000.;

题目大意

删除一棵二叉树中的所有值出现在to_delete中的节点。

解题方法

递归

参考了lee215大神的答案。看到二叉树的题就想到递归呀!

一个节点被删除时有以下几个情况:

1、 如果该节点是根节点,形成左右两个子树,此时递归左右子树;
2、 如果该节点不是根节点,那么需要修改其父节点指向自己的指针为空,并且递归左右子树;

一个节点一旦被删除,那么其左右孩子就是新的树的根节点。 如果一个节点是根节点,并且不被删除的情况下,才会放入结果中。

C++代码如下:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {
        vector<TreeNode*> res;
        helper(root, true, res, to_delete);
        return res;
    }
    void helper(TreeNode*& node, bool isRoot, vector<TreeNode*>& res, vector<int>& to_delete) {
        if (!node) return;
        bool isDel = delCurNode(node, to_delete);
        helper(node->left, isDel, res, to_delete);
        helper(node->right, isDel, res, to_delete);
        if (isRoot && !isDel) {
            res.push_back(node);
        }
        if (!isRoot && isDel) {
            node = nullptr;
        }
    }
    bool delCurNode(TreeNode* root, vector<int>& to_delete) {
        for (int val : to_delete) {
            if (root->val == val) {
                return true;
            }
        }
        return false;
    }
};

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

参考资料:https://leetcode.com/problems/delete-nodes-and-return-forest/discuss/328853/JavaC%2B%2BPython-Recursion-Solution

2022

DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有

本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发