Skip to content

Commit

Permalink
0701 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed May 1, 2019
1 parent c2f0e19 commit a0bce42
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 0701-Insert-into-a-Binary-Search-Tree/cpp-0701/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.14)
project(cpp_0701)

set(CMAKE_CXX_STANDARD 14)

add_executable(cpp_0701 main.cpp)
39 changes: 39 additions & 0 deletions 0701-Insert-into-a-Binary-Search-Tree/cpp-0701/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/// Source : https://leetcode.com/problems/insert-into-a-binary-search-tree/
/// Author : liuyubobobo
/// Time : 2019-04-30

#include <iostream>

using namespace std;


/// Recursive
/// Time Complexity: O(h)
/// Space Complexity: O(h)

/// 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) return new TreeNode(val);

if(val < root->val) root->left = insertIntoBST(root->left, val);
else root->right = insertIntoBST(root->right, val);
return root;
}
};


int main() {

return 0;
}
54 changes: 54 additions & 0 deletions 0701-Insert-into-a-Binary-Search-Tree/cpp-0701/main2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/// Source : https://leetcode.com/problems/insert-into-a-binary-search-tree/
/// Author : liuyubobobo
/// Time : 2019-04-30

#include <iostream>

using namespace std;


/// Iterative
/// Time Complexity: O(h)
/// Space Complexity: O(h)

/// 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) return new TreeNode(val);

TreeNode* p = root;
while(true){
if(val < p->val){
if(!p->left){
p->left = new TreeNode(val);
break;
}
p = p->left;
}
else{
if(!p->right){
p->right = new TreeNode(val);
break;
}
p = p->right;
}
}
return root;
}
};


int main() {

return 0;
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ email: [[email protected]](mailto:[email protected])
| 698 | [Partition to K Equal Sum Subsets](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/) | | [C++](0698-Partition-to-K-Equal-Sum-Subsets/cpp-0698/) | [Java](0698-Partition-to-K-Equal-Sum-Subsets/java-0698/src/) | |
| 699 | [Falling Squares](https://leetcode.com/contest/leetcode-weekly-contest-54/problems/falling-squares/) | [缺:线段树;块状链表] | [C++](0699-Falling-Squares/cpp-0699/) | | |
| | | | | | |
| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [solution](https://leetcode.com/problems/insert-into-a-binary-search-tree/solution/) | [C++](0701-Insert-into-a-Binary-Search-Tree/cpp-0701/) | | |
| | | | | | |
| 704 | [Binary Search](https://leetcode.com/problems/binary-search/description/) | [] | [C++](0704-Binary-Search/cpp-0704/) | | |
| 705 | [Design HashSet](https://leetcode.com/problems/design-hashset/description/) | [] | [C++](0705-Design-HashSet/cpp-0705/) | | |
| 706 | [Design HashMap](https://leetcode.com/problems/design-hashmap/description/) | [] | [C++](0706-Design-HashMap/cpp-0706/) | | |
Expand Down

0 comments on commit a0bce42

Please sign in to comment.