| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- C++
- omscs
- MATLAB
- zeros
- 코딩테스트
- 딥러닝
- 주정부이민
- 개발자
- 선형대수
- 컴퓨터과학과
- 방통대
- 프로그래머스
- 위니펙
- 매트랩
- LV1
- LAA
- EOI
- 머신러닝
- leetcode
- cpp
- 조지아텍
- 캐나다 영주권
- machine learning
- 방송통신대학교
- mpnp
- 기본
- Deep learning
- Plotting
- 알고리즘
- 온라인석사
Archives
- Today
- Total
Byte by Byte
894. All Possible Full Binary Trees.cpp 본문
어려워서 남이 풀어놓은 것을 보고 외워서 작성함.
아직은 혼자 짜라고하면 못짤듯하나, recursive를 따라가본 정도로 만족.
//spend 7hours to understand. (still not fully understood..)
//this is not my own-created code.
//thanks to https://leetcode.com/problems/all-possible-full-binary-trees/discuss/1221826/C%2B%2B-Easy-Recursive-No-Extra-Function-Needed-(With-Explanation)
/**
* 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:
vector<TreeNode*> allPossibleFBT(int n) {
vector<TreeNode*>res;
if(n==1){
TreeNode* root = new TreeNode();
res.push_back(root);
return res;
}
for(int i=1; i<=n-2; i+=2){
vector<TreeNode*>left = allPossibleFBT(i);
vector<TreeNode*>right = allPossibleFBT(n-1-i);
for(int j=0; j<left.size();++j){
for(int k=0; k<right.size();++k){
TreeNode* root = new TreeNode();
root->left = left[j];
root->right = right[k];
res.push_back(root);
}
}
}
return res;
}
};'개발 로그 > 알고리즘' 카테고리의 다른 글
| 1252. Cells with Odd Values in a Matrix.cpp (0) | 2021.10.02 |
|---|---|
| 1725. Number Of Rectangles That Can Form The Largest Square.cpp (0) | 2021.10.02 |
| 1008. Construct Binary Search Tree from Preorder Traversal.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 |