1. 程式人生 > >701. Insert into a Binary Search Tree的C++解法

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

簡單的遞迴。

 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;
		 }
	 }
 };