leetcode 1110. Delete Nodes And Return Forest

mac2022-06-30  35

/** * 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; func(root,true,res,to_delete); return res; } TreeNode* func(TreeNode* root,bool is_root,vector<TreeNode*>& res,vector<int>& to_delete){ bool is_in=false; if(!root)return NULL; for(auto num:to_delete) { if(num==root->val) { is_in=true; break; } } if(is_root&&!is_in) res.push_back(root); root->left=func(root->left,is_in,res,to_delete); root->right=func(root->right,is_in,res,to_delete); if(is_in) return NULL; else return root; } };
最新回复(0)