1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Solution { public: bool leafSimilar(TreeNode* root1, TreeNode* root2) { vector<int> seq1, seq2; dfs(root1, seq1); dfs(root2, seq2); return seq1 == seq2; } void dfs(TreeNode* root, vector<int>& seq) { if (root) { if (root->left == nullptr && root->right == nullptr) { seq.emplace_back(root->val); } else { dfs(root->left, seq); dfs(root->right, seq); } } } };
|