LeetCode刷题笔记--100. Same Tree

100. Same Tree

Easy

101531FavoriteShare

Given two binary trees, 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:

Input:     1         1
          / \       / \
         2   3     2   3

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

Output: true

Example 2:

Input:     1         1
          /           \
         2             2

        [1,2],     [1,null,2]

Output: false

Example 3:

Input:     1         1
          / \       / \
         2   1     1   2

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

Output: false

终于开始刷二叉树的题目了。一开始真的不懂,还好网上能找到一些资料。感谢下面大神的文章,给我一些启发:

https://blog.csdn.net/bg2bkk/article/details/37572499

在网页上跑不出来的时候确实需要VS调试一下。原本就是一道很简单,很基础的题目,认认真真做了,还是可以学点东西。下面是我AC的代码:

/**
 * 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;
        if(p==NULL||q==NULL)return false;
        else if(p->left==NULL&&p->right==NULL&&q->left==NULL&&q->right==NULL)
        {
            if(p->val==q->val)return true;
            else return false;
        }
        else
        {
            if (p->val != q->val)return false;//这一句一开始漏了,进VS调才发现这个问题,即使不是叶子节点也是需要判断是否相同的,如果不相同的话直接返回false,就不再进行递归比较了。
            else
            {
                return (isSameTree(p->left,q->left)&&isSameTree(p->right,q->right));
            }
            
        }
        
    }
};

我VS上调试的代码如下:

struct TreeNode {
        int val;
        TreeNode *left;
        TreeNode *right;
        TreeNode(int x) : val(x), left(NULL), right(NULL) {}
        
    };

    TreeNode *constructTree(char *dat, int len)
    {
        TreeNode *root = NULL;
        int index = 0;
        if (len > 0)
            root = new TreeNode(dat[index] - '0');
        else
            return NULL;

        list<TreeNode *> node;
        node.push_back(root);
        index++;
        while (index < len)
        {
            if (!node.empty())
            {
                TreeNode *root = node.front();
                if (index < len)
                {
                    if (dat[index] != '#')
                    {
                        root->left = new TreeNode(dat[index] - '0');
                        node.push_back(root->left);
                    }
                    index++;
                }
                if (index < len)
                {
                    if (dat[index] != '#')
                    {
                        root->right = new TreeNode(dat[index] - '0');
                        node.push_back(root->right);
                    }
                    index++;
                }
                node.pop_front();
            }
        }

        return root;
    }


    bool isSameTree(TreeNode* p, TreeNode* q) {
        if (p == NULL && q == NULL)return true;
        else if (p == NULL && q != NULL)return false;
        else if (q == NULL && p != NULL)return false;
        else if (p->left == NULL && p->right == NULL && q->left == NULL && q->right == NULL)
        {
            if (p->val == q->val)return true;
            else return false;
        }
        else
        {
            if (p->val != q->val)return false;
            else
            {
                return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
            }
        }

    }

int main()
{
    std::cout << "Hello World!\n";
    //TreeNode *root = NULL;//看注掉的这一片,一开始是如此笨的方法,不过这样写过以后,感觉对二叉树更理解一点了。
    //root = new TreeNode(1);//二叉树根
    //list<TreeNode *> node;
    //node.push_back(root);
    ///*root->left = new TreeNode(NULL);
    //node.push_back(root->left);*/
    //root->right = new TreeNode(3);
    //node.push_back(root->right);

    //TreeNode *root1 = NULL;
    //root1 = new TreeNode(1);//二叉树根
    //list<TreeNode *> node1;
    //node1.push_back(root1);
    //root1->left = new TreeNode(2);
    //node.push_back(root1->left);
    //root1->right = new TreeNode(3);
    //node1.push_back(root1->right);

    //bool p = isSameTree(root,root1);

    TreeNode *root = NULL;
    TreeNode *root1 = NULL;
    char dd[] = "1#24##3";
    root = constructTree(dd,7);
    char dd1[] = "1#42##3";
    root1 = constructTree(dd1, 7);
    bool p = isSameTree(root, root1);
    std::cout << "isSameTree"<<p;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]和引用\[2\]的内容,推荐的LeetCode刷题顺序是按照题目类型刷题,优先选择树、链表、二分查找、DFS、BFS、动态规划等常见类型的题目。可以先做2~4道简单题,然后再做中等难度的题目。在选择题目时,可以优先选择题目序号小、点赞多、提交成功率高的题目,这样可以从简单入手,节省时间。同时,LeetCode每道题目都有“模拟面试”功能,可以给自己设定时间限制,如果做不出来可以看答案,然后记住思路后再自己尝试一遍。每种类型的题目做完10+道后,可以总结规律。 根据引用\[3\]的内容,题目可以按照不同的分类进行刷题,比如数组与贪心算法、子数组与贪心算法、子序列与贪心算法、数字与贪心、单调栈法、双指针法等。可以根据自己的兴趣和需求选择相应的题目进行刷题。 综上所述,LeetCode刷题顺序可以按照题目类型或者题目分类进行选择。 #### 引用[.reference_title] - *1* [LeetCode 刷题顺序,按标签分类,科学刷题!](https://blog.csdn.net/fengyuyeguirenenen/article/details/125099023)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [leetcode 刷题指南 & 刷题顺序](https://blog.csdn.net/qijingpei/article/details/125561071)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [leetcode-刷题顺序推荐](https://blog.csdn.net/weixin_38087674/article/details/114107841)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值