【leetcode】树—简单(100、相同的树 Same Tree)

100、给定两个二叉树,编写一个函数来检验它们是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

示例 1:

输入:       1           1
               / \         / \
             2   3     2   3

        [1,2,3],   [1,2,3]

输出: true

我的思路:用前序遍历两个树,遍历的结果存入vector,比较两次遍历结果是否相同。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
 #include<vector>
class Solution {
public:
    vector<vector<int> > vec;
    void preorder(TreeNode *p,int i){
        if(p==NULL) return;
        vec[i].push_back(p->val);
        if(p->left!=NULL) preorder(p->left,i);
        else vec[i].push_back(-1);
        if(p->right!=NULL) preorder(p->right,i);
        else vec[i].push_back(-1);
    }
    bool isSameTree(TreeNode* p, TreeNode* q) {
        vec.resize(2);
        preorder(p,0);
        preorder(q,1);
        int len1=vec[0].size(),len2=vec[1].size();
        if(len1!=len2) return false;
        else{
            for(int i=0;i<len1;i++){
                if(vec[0][i]!=vec[1][i]) return false;
            }
            return true;
        }

    }
};

 运行结果:

执行用时 :4 ms, 在所有 C++ 提交中击败了51.96%的用户

内存消耗 :8.2 MB, 在所有 C++ 提交中击败了100.00%的用户

 

修改1:直接将isSame()写成递归形式

/**
 * 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 isSameTree(TreeNode* p, TreeNode* q) {
        if(p==NULL && q==NULL) return true;
        else if(p==NULL && q!=NULL) return false;
        else if(p!=NULL && q==NULL) return false;
        else if(p->val==q->val){
        if(isSameTree(p->left,q->left) && isSameTree(p->right,q->right)) return true;
        else return false;
        }
        else return false;
    }
};

结果:

执行用时 :4 ms, 在所有 C++ 提交中击败了51.96%的用户

内存消耗 :7.5 MB, 在所有 C++ 提交中击败了100.00%的用户

 

对修改1代码进行优化,参考大佬的代码:

/**
 * 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 isSameTree(TreeNode* p, TreeNode* q) {
        if(p==NULL && q==NULL) return true;
        else if(p==NULL || q==NULL) return false;//合并p=NULL&&q!=NULL 和p!=NULL&&q==NULL
        else if (p->val != q->val)return false;//此时p!=NULL&&a!=NULL,仅当当前节点相同才进行下一轮递归。
        return (isSameTree(p->left,q->left) && isSameTree(p->right,q->right));
    }
};

执行用时 :0 ms, 在所有 C++ 提交中击败了100.00%的用户

内存消耗 :7.7 MB, 在所有 C++ 提交中击败了100.00%的用户

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值