Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- vector<TreeNode*> splitBST(TreeNode* root, int V) {
- return dfs(root, V);
- }
- vector<TreeNode*> dfs(TreeNode* root, int V){
- vector<TreeNode*> res(2, NULL);
- if(!root) return res;
- if(root->val > V){
- res[1] = root;
- auto tmp = dfs(root->left, V);
- root->left = tmp[1];
- res[0] = tmp[0];
- }
- else{
- res[0] = root;
- auto tmp = dfs(root->right, V);
- root->right = tmp[0];
- res[1] = tmp[1];
- }
- return res;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement