Advanced binary tree problems

776. Split BST

Solve it recursively

* 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 {
vector<TreeNode*> splitBST(TreeNode* root, int V) {
vector<TreeNode*> ret;
if (!root) return {nullptr, nullptr};
if (root->val == V) {
auto right = root->right;
root->right = nullptr;
return ret;
else {
if (root->val > V) {
auto left = splitBST(root->left, V);
root->left = left[1];
return ret;
else {
auto right = splitBST(root->right, V);
root->right = right[0];
return ret;




Data Scientist/MLE/SWE @takemobi

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Burndown Chart… Or not?

| Engineering News-Record

Haskell — the purely functional Lang!

Class & Objects Basics

FAIR Software at the 2019 eScience Symposium

LANGSEC in Software Development

12 Advantages Of Learning Python Over Other Languages

Simulate the whole process

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jimmy Shen

Jimmy Shen

Data Scientist/MLE/SWE @takemobi

More from Medium

Graph Algorithm — Cycle Detection in Undirected Graph using BFS

Round-Robin Scheduling Algorithm

Leetcode Q519. Random Flip Matrix (Q438)

[LeetCode] (Easy) 83. Remove Duplicates from Sorted List