Guest User

Untitled

a guest
Jan 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  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. };
Add Comment
Please, Sign In to add comment