思路:
直接前序遍历两个树获得叶子节点,然后对vector容器进行比较
class Solution {
public:
bool leafSimilar(TreeNode* root1, TreeNode* root2) {
vector<int> r1,r2;
getLeaf(root1,r1);
getLeaf(root2,r2);
return r1==r2;
}
void getLeaf(TreeNode* root,vector<int>& ans){
if(!root->left && !root->right){
ans.push_back(root->val);
return;
}
if(root->left)
getLeaf(root->left,ans);
if(root->right)
getLeaf(root->right,ans);
}
};
标签:叶子,right,TreeNode,r1,getLeaf,872,力扣,ans,root From: https://www.cnblogs.com/Syukuu/p/16725754.html