码迷,mamicode.com
首页 > 其他好文 > 详细

701. Insert into a Binary Search Tree

时间:2018-10-04 17:23:53      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:efi   arc   node   div   arch   struct   tin   插入   into   

经典题目:给定一个二叉搜索树,插入一个值为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

标签:efi   arc   node   div   arch   struct   tin   插入   into   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!