Advertisement
nikunjsoni

701

May 9th, 2021
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 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.     TreeNode* insertIntoBST(TreeNode* root, int val) {
  15.         if(!root) return new TreeNode(val);
  16.         if(root->val < val){
  17.             root->right = insertIntoBST(root->right, val);
  18.         }
  19.         else{
  20.             root->left = insertIntoBST(root->left, val);
  21.         }
  22.         return root;
  23.     }
  24. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement