일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- machine learning
- C++
- zeros
- mpnp
- Plotting
- 컴퓨터과학과
- LV1
- 알고리즘
- EOI
- 방송통신대학교
- 선형대수
- cpp
- 기본
- LAA
- 온라인석사
- 프로그래머스
- 주정부이민
- 조지아텍
- 캐나다 영주권
- 방통대
- 개발자
- Deep learning
- MATLAB
- 머신러닝
- omscs
- 코딩테스트
- 마니토바
- 매트랩
- 딥러닝
- leetcode
Archives
- Today
- Total
Krononberg
701. Insert into a Binary Search Tree.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* insertIntoBST(TreeNode* root, int val) {
if(root==nullptr) return new TreeNode(val);
TreeNode* curr = root;
while(true){
if(curr->val > val){
if(curr->left != nullptr){
curr = curr->left;
}
else{
curr->left = new TreeNode(val);
break;
}
}
else{
if(curr->right != nullptr){
curr = curr->right;
}
else{
curr->right = new TreeNode(val);
break;
}
}
}
return root;
}
};
'개발 로그 > 알고리즘' 카테고리의 다른 글
861. Score After Flipping Matrix.cpp (0) | 2021.10.12 |
---|---|
1325. Delete Leaves With a Given Value.cpp (0) | 2021.10.12 |
1641. Count Sorted Vowel Strings.cpp (0) | 2021.10.08 |
1829. Maximum XOR for Each Query.cpp (0) | 2021.10.08 |
1748. Sum of Unique Elements.cpp (0) | 2021.10.08 |