/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
insert(root, new TreeNode(val));
return root;
}
private:
TreeNode* insert(TreeNode* root, TreeNode* node) {
if (root == NULL) {
return node;
}
if (node->val <= root->val) {
root->left = insert(root->left, node);
} else {
root->right = insert(root->right, node);
}
return root;
}
};