일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 선형대수
- leetcode
- Plotting
- 방송통신대학교
- 딥러닝
- C++
- 캐나다 영주권
- 주정부이민
- LAA
- 알고리즘
- 온라인석사
- cpp
- 머신러닝
- MATLAB
- LV1
- 프로그래머스
- zeros
- machine learning
- 기본
- EOI
- 방통대
- 개발자
- omscs
- 코딩테스트
- 조지아텍
- mpnp
- 컴퓨터과학과
- Deep learning
- 매트랩
- 마니토바
Archives
- Today
- Total
Krononberg
1008. Construct Binary Search Tree from Preorder Traversal.cpp 본문
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* newNode(int x){
TreeNode* tmp = new TreeNode(x);
return tmp;
}
TreeNode* insert(TreeNode* root, int x){
if(root==nullptr){
return newNode(x);
}
if(root->val > x){
root->left = insert(root->left,x);
}
else
root->right = insert(root->right,x);
return root;
}
TreeNode* bstFromPreorder(vector<int>& preorder) {
TreeNode* root = nullptr;
for(auto value : preorder){
root = insert(root,value);
}
return root;
}
};
'개발 로그 > 알고리즘' 카테고리의 다른 글
1725. Number Of Rectangles That Can Form The Largest Square.cpp (0) | 2021.10.02 |
---|---|
894. All Possible Full Binary Trees.cpp (0) | 2021.10.02 |
1817. Finding the Users Active Minutes.cpp (0) | 2021.09.30 |
797. All Paths From Source to Target.cpp (0) | 2021.09.30 |
804. Unique Morse Code Words.cpp (0) | 2021.09.29 |