https://leetcode.com/problems/binary-tree-level-order-traversal/description/
주어진 binary tree를 level order 탐색하라.
사실상 easy 난이도 문제.
BS의 기본 문제, 다른 문제들로 응용에 잘 나옴.
코드
더보기
vector<vector<int>> levelOrder(TreeNode* root)
{
vector<vector<int>> vRet;
if (root == nullptr)
return vRet;
queue<TreeNode*> qBuff;
qBuff.push(root);
while (qBuff.size() > 0)
{
vector<int> vBuff;
int cnt = qBuff.size();
for (int q = 0; q < cnt; ++q)
{
TreeNode* node = qBuff.front();
qBuff.pop();
vBuff.push_back(node->val);
if (node->left != nullptr) qBuff.push(node->left);
if (node->right != nullptr) qBuff.push(node->right);
}
vRet.push_back(vBuff);
}
return vRet;
}
결과
'Leetcode > NeetCode' 카테고리의 다른 글
[BinaryTree][Medium] 1448. Count Good Nodes in Binary Tree (0) | 2024.07.18 |
---|---|
[BinaryTree][Medium] 199. Binary Tree Right Side View (0) | 2024.07.18 |
[BinaryTree][Medium] 235. Lowest Common Ancestor of a Binary Search Tree (0) | 2024.07.17 |
[BinaryTree][Easy] 572. Subtree of Another Tree (0) | 2024.07.17 |
[BinaryTree][Easy] 100. Same Tree (0) | 2024.07.17 |