Skip to content

Commit

Permalink
이슈 #113에서 솔루션 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Oct 17, 2024
1 parent 701c3cc commit 1ea199e
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions LeetCode/Leaf-Similar_Trees.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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;
}
};

0 comments on commit 1ea199e

Please sign in to comment.