• 【Leetcode】 100. 相同的树


    Given the roots of two binary trees p and q, write a function to check if they are the same or not.

    Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.

    Example 1:
    e1

    Input: p = [1,2,3], q = [1,2,3]
    Output: true
    
    • 1
    • 2

    Example 2:
    e2

    Input: p = [1,2], q = [1,null,2]
    Output: false
    
    • 1
    • 2

    Example 3:
    e3

    Input: p = [1,2,1], q = [1,1,2]
    Output: false
    
    • 1
    • 2

    Constraints:

    The number of nodes in both trees is in the range [0, 100].
    -104 <= Node.val <= 104
    
    • 1
    • 2

    Thought:
    BFS)使用两个队列分别存储两个二叉树的节点,然后逐个比较它们的值以及左右子树是否相同。如果两个节点的值不同或者它们的左右子树的情况不同,就说明这两个二叉树不相同。如果两个队列都为空,说明两个二叉树完全相同。

    AC:

    /*
     * @lc app=leetcode.cn id=100 lang=cpp
     *
     * [100] 相同的树
     */
    
    // @lc code=start
    /**
     * 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 {
    public:
        bool isSameTree(TreeNode* p, TreeNode* q) {
            if(p == nullptr && q == nullptr)
            {
                return true;
            }
            else if(p == nullptr || q == nullptr)
            {
                return false;
            }
            queue <TreeNode*> queue1, queue2;
            queue1.push(p);
            queue2.push(q);
            while(!queue1.empty() && !queue2.empty())
            {
                auto node1 = queue1.front();
                queue1.pop();
                auto node2 = queue2.front();
                queue2.pop();
                if(node1->val != node2->val)
                {
                    return false;
                }
                auto left1 = node1->left, right1 = node1->right, left2 = node2->left, right2 = node2->right;
                if((left1 == nullptr) ^ (left2 == nullptr))
                {
                    return false;
                }
                if((right1 == nullptr) ^ (right2 == nullptr))
                {
                    return false;
                }
                if(left1 != nullptr)
                {
                    queue1.push(left1);
                }
                if(right1 != nullptr)
                {
                    queue1.push(right1);
                }
                if(left2 != nullptr)
                {
                    queue2.push(left2);
                }
                if(right2 != nullptr)
                {
                    queue2.push(right2);
                }
            }
            return queue1.empty() && queue2.empty();
        }
    };
    // @lc code=end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    AC

  • 相关阅读:
    7 步保障 Kubernetes 集群安全
    前端飞机大战小游戏
    Baize_h1mini六足机器人零件准备
    【Java笔试强训】Day9(CM72 另类加法、HJ91 走方格的方案数)
    【zookeeper】zk的ZAB原子广播协议
    快速入门EasyX图形编程
    Unity 3D模型展示框架篇之Addressables+ILRuntime热更(完结篇)
    唯品会常用的两个API接口:关键字搜索API、获取商品详情数据API
    SSL---VPN
    java实现的截取网页图片的方式
  • 原文地址:https://blog.csdn.net/qq_54053990/article/details/131340363