일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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#
- type
- leetcode
- AI
- docker
- machine learning
- property
- vuetify
- webpack
- JavaScript
- condition
- 앙상블
- VUE
- BOJ
- git
- C++
- 보안
- scss
- generic
- security
- nginx
- TypeScript
- Clone
- vue.js
- Python
- dotenv
- var
- loop
- npm
- bash
Archives
- Today
- Total
ice rabbit programming
[LeetCode] Minimum Depth of Binary Tree 본문
728x90
https://leetcode.com/problems/minimum-depth-of-binary-tree/
Minimum Depth of Binary Tree - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
이진 트리에서 최소 깊이를 가진 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];
}
};
728x90
'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 |