LeetCode—872. Leaf-Similar Trees


//url:https://leetcode.com/problems/leaf-similar-trees/description/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool leafSimilar(TreeNode* root1, TreeNode* root2) {
        vector v1,v2;
        getLeafVec(root1,v1);
        getLeafVec(root2,v2);
        int v1size=v1.size();
        int v2size=v2.size();
        if(v1size!=v2size)
            return false;
        for(int i=0;i& vec){
        if(!root)
        {
            return;
        }
        if(!root->left&&!root->right){
            vec.push_back(root->val);
        }
        getLeafVec(root->left,vec);
        getLeafVec(root->right,vec);
    }
};


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注