일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- docker
- 보안
- leetcode
- npm
- vue.js
- VUE
- git
- webpack
- var
- Python
- type
- JavaScript
- loop
- AI
- TypeScript
- condition
- dotenv
- vuetify
- Clone
- 앙상블
- BOJ
- scss
- machine learning
- security
- nginx
- C++
- C#
- bash
- property
- generic
Archives
- Today
- Total
ice rabbit programming
[LeetCode] Minimum Depth of Binary Tree 본문
https://leetcode.com/problems/minimum-depth-of-binary-tree/
이진 트리에서 최소 깊이를 가진 leaf node의 depth를 구하는 문제이다. 기본적인 BFS 문제라고 생각한다.
/**
* 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:
int minDepth(TreeNode* root) {
if(root==nullptr)
return 0;
int index=0;
queue<TreeNode*> q;
vector<int> d;
d.push_back(1);
q.push(root);
while(!q.empty()) {
TreeNode* temp = q.front();
q.pop();
if(temp->right==nullptr && temp->left==nullptr)
return d[index];
if(temp->right!=nullptr) {
q.push(temp->right);
d.push_back(d[index]+1);
}
if(temp->left!=nullptr) {
q.push(temp->left);
d.push_back(d[index]+1);
}
index++;
}
return d[index];
}
};
'PS > LeetCode' 카테고리의 다른 글
[LeetCode] Pascal's Triangle (0) | 2020.04.20 |
---|---|
[LeetCode] Maximum Depth of Binary Tree (feat. 고정관념?) (0) | 2020.04.20 |
[LeetCode] Best Time to Buy and Sell Stock (0) | 2020.04.19 |
[LeetCode] Two Sum II - Input array is sorted (0) | 2020.04.18 |
[LeetCode] Linked List Cycle (0) | 2020.04.16 |