leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0701.cpp (403B)


      1 class Solution {
      2   public:
      3     TreeNode *insertIntoBST(TreeNode *root, int val) {
      4         if (!root) return new TreeNode(val);
      5 
      6         TreeNode *prev = nullptr;
      7         for (TreeNode *tmp = root; tmp;) {
      8             prev = tmp;
      9             tmp = val < tmp->val ? tmp->left : tmp->right;
     10         }
     11 
     12         (val > prev->val ? prev->right : prev->left) = new TreeNode(val);
     13         return root;
     14     }
     15 };