题目链接
https://leetcode-cn.com/problems/leaf-similar-trees/
个人实现:
#include <vector>
class Solution {
private:
void dfs(TreeNode * root, vector<int> &v){
if (root==nullptr)return;
dfs(root->left,v);
if(root->left==nullptr && root->right==nullptr)v.push_back(root->val);
dfs(root->right,v);
}
public:
bool leafSimilar(TreeNode* root1, TreeNode* root2) {
vector<int> v1,v2;
dfs(root1,v1); //进行树中序遍历,取其中的叶节点
dfs(root2,v2); //进行树中序遍历,取其中的叶节点
return v1==v2;
}
};