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

Leaf-Similar Trees #113

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

Leaf-Similar Trees #113

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:
    bool leafSimilar(TreeNode* root1, TreeNode* root2) {
        vector<int> root1Leaf;
        vector<int> root2Leaf;

        returnLeaf(root1, root1Leaf);
        returnLeaf(root2, root2Leaf);

        return root1Leaf == root2Leaf;
    }

private:
    void returnLeaf(TreeNode* node, vector<int>& v) {
        if (node == nullptr) {
            return;
        }

        if (node->left == nullptr && node->right == nullptr) {
            v.push_back(node->val);
        }

        returnLeaf(node->left, v);
        returnLeaf(node->right, v);

        return;
    }
};
  • DFS문제로 단순하게 함수 만들어서 풀이
  • vector끼리 ==연산에 대해 좀 더 깊게 알게 됨.. 반복으로 비교할 필요가 없음
@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 Two-Week Plan to Done in Todo Oct 17, 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