forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2f0e19
commit a0bce42
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
0701-Insert-into-a-Binary-Search-Tree/cpp-0701/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/) | | | | ||
|