Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary Tree Right Side View #118

Closed
Tracked by #101
fkdl0048 opened this issue Oct 15, 2024 · 0 comments
Closed
Tracked by #101

Binary Tree Right Side View #118

fkdl0048 opened this issue Oct 15, 2024 · 0 comments
Assignees
Labels
Milestone

Comments

@fkdl0048
Copy link
Owner

fkdl0048 commented Oct 15, 2024

/**
 * 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<int> rightSideView(TreeNode* root) {
        vector<int> result;
        if (!root) return result;

        queue<TreeNode*> q;
        q.push(root);

        while(!q.empty()) {
            int levelSize = q.size();
            int rightmostValue;

            for (int i = 0; i < levelSize; ++i) {
                TreeNode* node = q.front();
                q.pop();
                rightmostValue = node->val;

                if (node->left) {
                    q.push(node->left);
                }
                if (node->right) {
                    q.push(node->right);
                }
            }

            result.push_back(rightmostValue);
        }

        return result;
    }
};
  • 처음에 문제를 잘못 이해해서 오른쪽으로만 내려가게 만들면 되는 줄 알았다가.. BFS의 형태의 탐색을 좀 더 넓게 봐야함을 깨닫게 된 문제..
  • 자료구조의 특성응 이해하고 이를 활용하는 능력(문제)
@fkdl0048 fkdl0048 mentioned this issue Oct 15, 2024
75 tasks
@fkdl0048 fkdl0048 self-assigned this Oct 15, 2024
@fkdl0048 fkdl0048 added this to Todo Oct 15, 2024
@github-project-automation github-project-automation bot moved this to Todo in Todo Oct 15, 2024
@fkdl0048 fkdl0048 added this to the LeetCode milestone Oct 15, 2024
@fkdl0048 fkdl0048 moved this from Todo to Two-Week Plan in Todo Oct 15, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in Todo Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

No branches or pull requests

1 participant