首页 > 其他 > 详细

701. Insert into a Binary Search Tree

时间:2018-10-04 17:18:43      阅读:186      评论:0      收藏:0      [点我收藏+]

经典题目:给定一个二叉搜索树,插入一个值为val的新节点

/**
 * 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) {
        if(root==NULL){
            root=new TreeNode(val);
            return root;
        }
        if(root->val<val){
            root->right=insertIntoBST(root->right,val);
        }else{
            root->left=insertIntoBST(root->left,val);
        }
        return root;
    }
};

 

701. Insert into a Binary Search Tree

原文:https://www.cnblogs.com/learning-c/p/9742570.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!