daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Jan 23rd, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.     TreeNode* insertIntoBST(TreeNode* root, int val) {
  4.         if (root == nullptr) return new TreeNode(val);
  5.         TreeNode* prev = nullptr;
  6.         for (auto node = root; node != nullptr; ) {
  7.             prev = node;
  8.             node = val < node->val ? node->left : node->right;
  9.         }
  10.         auto destination = val < prev->val ? &prev->left : &prev->right;
  11.         *destination = new TreeNode(val);
  12.         return root;
  13.     }
  14. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top