LeetCode:701. 二叉搜索树中的插入操作

    技术2022-07-15  52

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