872. 叶子相似的树

思路:
最直观的就是分别用两个数组来存放两棵树的叶子节点。
所以就是dfs每棵树,当遍历到叶子节点时,就加入进数组里。最后得到的两个数组在判断长度是否相等,不等就return false,相等就遍历判断是否存在不相等的元素,有就return false
代码:
DFS递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
    vector<int> r1;
    vector<int> r2;
public:
    void dfs(TreeNode* root,int idx){
        if(!root->left&&!root->right) {
            if(idx==1) r1.push_back(root->val);
            else r2.push_back(root->val);
            return;
        }
        if(root->left) dfs(root->left,idx);
        if(root->right) dfs(root->right,idx);
        return;
    }
    bool leafSimilar(TreeNode* root1, TreeNode* root2) {
        dfs(root1,1);
        dfs(root2,2);
        int l1=r1.size();
        int l2=r2.size();
        if(l1!=l2) return false;
        for(int i=0;i<l1;++i){
            if(r1[i]!=r2[i]) return false;
        }
        return true;
    }
};

DFS迭代

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
    vector<int> r1;
    vector<int> r2;
public:
    bool leafSimilar(TreeNode* root1, TreeNode* root2) {
        stack<TreeNode*> s1;
        stack<TreeNode*> s2;
        s1.push(root1);
        s2.push(root2);
        while(!s1.empty()){
            TreeNode* node = s1.top(); s1.pop();
            if(node->left) s1.push(node->left);
            if(node->right) s1.push(node->right);
            if(!node->left&&!node->right) r1.push_back(node->val);
        }
        while(!s2.empty()){
            TreeNode* node = s2.top(); s2.pop();
            if(node->left) s2.push(node->left);
            if(node->right) s2.push(node->right);
            if(!node->left&&!node->right) r2.push_back(node->val);
        }
        int l1=r1.size(),l2=r2.size();
        if(l1!=l2) return false;
        for(int i=0;i<l1;++i){
            if(r1[i] != r2[i]) return false;
        }
        return true;
    }
};

这道题没法用bfs解决,因为叶子节点的深度会不同,BFS会乱序,即使叶子节点元素顺序相同,但BFS加入进数组后顺序不相同。
可用如下代码去分析。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
    vector<int> r1;
    vector<int> r2;
public:
    bool leafSimilar(TreeNode* root1, TreeNode* root2) {
        queue<TreeNode*> q1;
        queue<TreeNode*> q2;
        q1.push(root1);
        q2.push(root2);
        while(!q1.empty()){
            TreeNode* node = q1.front(); q1.pop();
            if(node->left) q1.push(node->left);
            if(node->right) q1.push(node->right);
            if(!node->left&&!node->right) r1.push_back(node->val);
        }
        while(!q2.empty()){
            TreeNode* node = q2.front(); q2.pop();
            if(node->left) q2.push(node->left);
            if(node->right) q2.push(node->right);
            if(!node->left&&!node->right) r2.push_back(node->val);
        }
        int l1=r1.size(),l2=r2.size();
        if(l1!=l2) return false;
        for(int i=0;i<l1;++i){
            if(r1[i] != r2[i]) return false;
        }
        return true;
    }
};
上一篇:872. 叶子相似的树(二叉树使用了Morris算法)


下一篇:872. 叶子相似的树