701. Insert into a Binary Search Tree的C++解法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/musechipin/article/details/82838896

简单的递归。

 class Solution {
 public:
	 TreeNode* insertIntoBST(TreeNode* root, int val) {
		 if (root == NULL) { TreeNode* thisnode = new TreeNode(val); return thisnode; }
		 else{
			 if (val > root->val) root->right = insertIntoBST(root->right,val);
			 else root->left = insertIntoBST(root->left,val);
			 return root;
		 }
	 }
 };

猜你喜欢

转载自blog.csdn.net/musechipin/article/details/82838896
今日推荐