python:
class Solution:
def leafSimilar(self, root1, root2):
"""
:type root1: TreeNode
:type root2: TreeNode
:rtype: bool
"""
def leaf(root):
if not root:
return []
if not (root.left or root.right):
return [root.val]
return leaf(root.left) + leaf(root.right)
return leaf(root1) == leaf(root2)
class Solution {
public:
bool leafSimilar(TreeNode* root1, TreeNode* root2) {
vector<int> le1;
vector<int> le2;
leaf(le1, root1);
leaf(le2, root2);
return le1 == le2;
}
void leaf(vector<int> &le, TreeNode* root){
if(root == nullptr)
return;
leaf(le, root->left);
if(root->left == nullptr && root->right == nullptr)
le.push_back(root->val);
leaf(le, root->right);
}
};