class Solution {
public:
bool leafSimilar(TreeNode* root1, TreeNode* root2) {
if(!root1 && !root2) return true;
vector<int> vecLeaf1;
vector<int> vecLeaf2;
getLeafNode(root1, vecLeaf1);
getLeafNode(root2, vecLeaf2);
return (vecLeaf1 == vecLeaf2);
}
private:
void getLeafNode(TreeNode* root, vector<int>& leafVal){
if(!root) return;
if(!root->left&&!root->right){
leafVal.push_back(root->val);
}
getLeafNode(root->left, leafVal);
getLeafNode(root->right, leafVal);
}
};
网友评论