leetcode100(相同的树)

相同的树

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

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

在这里插入图片描述
在这里插入图片描述

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


*/前序遍历
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        stack<TreeNode*> s1,s2;
        if(p) s1.push(p);
        if(q) s2.push(q);
        while(!s1.empty() && !s2.empty()){
            TreeNode *t1 = s1.top(); s1.pop();
            TreeNode *t2 = s2.top(); s2.pop();
            if(t1->val != t2->val) return false;
            if(t1->right) s1.push(t1->right);
            if(t2->right) s2.push(t2->right);
            if(s1.size() != s2.size()) return false;
            if(t1->left) s1.push(t1->left);
            if(t2->left) s2.push(t2->left);
            if(s1.size() != s2.size()) return false;
        
        }
        return s1.size() == s2.size();
    }
};


*/先序遍历
class Solution {
public:
    bool isSameTree(TreeNode *p, TreeNode *q) {
        stack<TreeNode *> sta;
        sta.push(p),sta.push(q);
        while(!sta.empty()){
            q=sta.top(),sta.pop();
            p=sta.top(),sta.pop();
            if(!p && !q) continue;
            if((!p && q) || (p && !q) || (p->val != q->val))return 0;
            sta.push(p->right),sta.push(q->right);
            sta.push(p->left),sta.push(q->left);
        }
        return 1;
    }
};


*/层序遍历
class Solution {
public:
    bool isSameTree(TreeNode *p, TreeNode *q) {
        queue<TreeNode *>que;
        que.push(p);
        que.push(q);
        while(!que.empty()) {
            p = que.front(),que.pop();
            q = que.front(),que.pop();
            if(!p && !q) continue;
            if((!p && q) || (p && !q) || p->val != q->val) return 0;
            que.push(p->left);
            que.push(q->left);
            que.push(p->right);
            que.push(q->right);
        }
        return 1;
    }
};





*/python 解法


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        if not p and not q:
            return True
        if not q or not p:
            return False
        if p.val != q.val:
            return False
        return self.isSameTree(p.right,q.right) and self.isSameTree(p.left,q.left)
        

*/
class Solution:
    def isSameTree(self,p,q):
        def check(p,q):
            if not p and not q:
                return True
            if not q or not p:
                return False
            if p.val != q.val:
                return False
            return True
        
        queue = [(p,q)]
        while len(queue):
            p,q = queue.pop(0)
            if not check(p,q):
                return False
            if p and q:
                queue.append((p.left,q.left))
                queue.append((p.right,q.right))
        return True
                
          
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值