一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

Given two binary trees, write a function to check if they are equal or not.

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

(二)解题

题目大意:比较两个二叉树是否相等

解题思路:采用深度优先搜索,依次遍历两个树,判断每个节点是否相等。

博主利用栈实现非递归的二叉输深度优先搜索,并判断每个节点

/**
 * 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) {
        stack<pair<TreeNode*,TreeNode*>> TwoTreeNode;//用栈实现非递归
        TwoTreeNode.push(make_pair<TreeNode*,TreeNode*>((TreeNode*)p,(TreeNode*)q));//初始化
        while(!TwoTreeNode.empty())
        {
            auto temp = TwoTreeNode.top();//去除栈顶
            TwoTreeNode.pop();//处理当前节点
            TreeNode* ptemp = temp.first;
            TreeNode* qtemp = temp.second;
            if(ptemp==NULL&&qtemp==NULL) continue;//两个都为空
            else if(ptemp!=NULL&&qtemp!=NULL){//两个都不为空
                if(ptemp->val==qtemp->val)//判断值是否相等
                {
                    TwoTreeNode.push(make_pair(ptemp->left,qtemp->left));//相等则放入栈等待处理
                    TwoTreeNode.push(make_pair(ptemp->right,qtemp->right));
                }
                else return false;//不相等返回false
            }
            else return false;//一个为空另一个不为空直接返回false
        }
        return true;//全部处理完都相等就返回true
    }
};

【一天一道LeetCode】#100. Same Tree(100题大关)的更多相关文章

  1. 100.Same Tree(E)

    100. same tree 100. Same Tree Given two binary trees, write a function to check if they are the same ...

  2. &lt;LeetCode OJ&gt; 100. Same Tree

    100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...

  3. LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number

    100. Same Tree /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; ...

  4. LeetCode 100. Same Tree (判断树是否完全相同)

    100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...

  5. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

  6. LeetCode解题录-51~100

    [leetcode]51. N-QueensN皇后    Backtracking Hard [leetcode]52. N-Queens II N皇后 Backtracking Hard [leet ...

  7. LeetCode高频题目(100)汇总-Java实现

    LeetCode高频题目(100)汇总-Java实现       LeetCode高频题目(100)汇总-Java实现 目录 第01-50题 [Leetcode-easy-1] Two Sum [Le ...

  8. 100. Same Tree(C++)

    100. Same Tree Given two binary trees, write a function to check if they are equal or not. Two binar ...

  9. 【一天一道LeetCode】#257. Binary Tree Paths

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. angular+ionic前后端分离开发项目中的使用

    Ionic基于AngularJS构建而成,所以学习一些AngularJS的知识很有必要.Ionic并没有独立开发一套完整的Web应用框架,而是对AngularJS进行了扩展,给它添加了大量界面组件和其 ...

  2. C++ 程序在运行时不显示dos界面

    在程序最开始处加一句: #pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" ) PS: 在VS中 ...

  3. python学习之路网络编程篇(第四篇)

    python学习之路网络编程篇(第四篇) 内容待补充

  4. Python OptionParser 使用详解(转载)

    Python使用命令行参数能使处理流程更自动化. 链接的内容讲解得十分详细:https://www.tuicool.com/articles/rUvIbi

  5. vue开发中v-for在Eslint的规则检查下出现:Elements in iteration expect to have 'v-bind:key' directives

    在使用VScode编辑器vue开发过程中,v-for在Eslint的规则检查下出现报错:Elements in iteration expect to have 'v-bind:key' direct ...

  6. 介绍Docker容器

    容器是 Docker 又一核心概念. 简单的说,容器是独立运行的一个或一组应用,以及它们的运行态环境.对应的,虚拟机可以理解为模拟运行的一整套操作系统(提供了运行态环境和其他系统环境)和跑在上面的应用 ...

  7. Swift基础之UIPickerView和小animate的使用

    写一个简单的UIPickerView的使用Demo,比较简单,其中和一个小动画的结合使用 UIPickerView的使用基本上跟OC语言中的一样,就是写法的样式问题,想必开发过OC的应该不需要多讲了, ...

  8. Python尾递归-创始人为何不愿TRE以及我们如何模拟TRE

    TRE=Tail Recursion Elimination 创始人是不愿意实现TRE的.他专门用了一篇文章来阐述原因. http://neopythonic.blogspot.com/2009/04 ...

  9. 使用QGIS将文本坐标转换为矢量文件

    本文主要是说明如果使用QGIS将文本格式的点坐标转换为矢量文件(如shapefile格式). 所需工具:QGIS 所需数据:文本格式的点文件 所需要处理的点坐标文件如下所示, 114.2 22.15 ...

  10. Freemarker 浅析

    今天分享一下一个模板语言的使用,它就是Freemarker,有点类似与前些日子做Python的Django中的模板语言,其实原理上都是相似的.所以这里就不对那些基础性的语法类的直至进行讲解了,就拿几个 ...