Advertisement
nikunjsoni

776

May 7th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12. class Solution {
  13. public:
  14.     vector<TreeNode*> splitBST(TreeNode* root, int V) {
  15.         return dfs(root, V);
  16.     }
  17.    
  18.     vector<TreeNode*> dfs(TreeNode* root, int V){
  19.         vector<TreeNode*> res(2, NULL);
  20.         if(!root) return res;
  21.         if(root->val > V){
  22.             res[1] = root;
  23.             auto tmp = dfs(root->left, V);
  24.             root->left = tmp[1];
  25.             res[0] = tmp[0];
  26.         }
  27.         else{
  28.             res[0] = root;
  29.             auto tmp = dfs(root->right, V);
  30.             root->right = tmp[0];
  31.             res[1] = tmp[1];
  32.         }
  33.         return res;
  34.     }
  35. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement