LeetCode872. 叶子相似的树

题目

 1 class Solution {
 2 public:
 3     vector<int>ans1;
 4     vector<int>ans2;
 5     bool leafSimilar(TreeNode* root1, TreeNode* root2) {
 6         dfs(root1,ans1);
 7         dfs(root2,ans2);
 8         return ans1 == ans2;
 9     }
10     void dfs(TreeNode* root, vector<int> &ans){
11         if(root!=NULL){
12         dfs(root->left,ans);
13         if(root->left == NULL && root->right == NULL) {ans.push_back(root->val);}
14         dfs(root->right,ans);
15         }
16     }
17 };

注意:传递参数用用地址,这样才能保存修改后的结果

原文地址:https://www.cnblogs.com/fresh-coder/p/14272235.html