一天一道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. jquery easyui datagrid设置可编辑行的某个列不可编辑

    function onClickRowd(index1, field1) { if (editIndexd != index1) { if (endEditing()) { $('#dg').data ...

  2. Eclipse 一直不停 building workspace完美解决总结

    一.产生这个问题的原因多种1.自动升级 2.未正确关闭  3.maven下载lib挂起 等.. 二.解决总结(1).解决方法        方法1.修改eclipse启动文件 eclipse.ini ...

  3. 【python进阶】Garbage collection垃圾回收1

    前言 GC垃圾回收在python中是很重要的一部分,同样我将分两次去讲解Garbage collection垃圾回收,此篇为Garbage collection垃圾回收第一篇,下面开始今天的说明~~~ ...

  4. 文件上传,服务端压缩文件方法,重点是png与gif图片的压缩,保证了透明度与动画

    /// <summary> /// 上传文件帮助类 /// </summary> public class ImageUploadHelper { #region SaveVi ...

  5. 基于Windows服务器,从0开始搭建一个基于RTSP协议的直播平台

    作案工具下载 EasyDarwin 服务端程序,用来接受推流和拉流 FFmpeg 可以用来推流视频数据到服务端,也可以从服务端拉流下来播放,也可以从一个服务端拉流下来,转推到另一个服务端去. Easy ...

  6. CSS中display:block属性的作用

    display:block可以理解为块,举个简单的例子!比如你做一个超链接,<li><a href="#">超链接</a></li> ...

  7. MySQL NULL 值处理

    MySQL NULL 值处理 我们已经知道MySQL使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作. 为了 ...

  8. Docker的Etcd项目

    etcd 是 CoreOS 团队发起的一个管理配置信息和服务发现(service discovery)的项目,在这一章里面,我们将介绍该项目的目标,安装和使用,以及实现的技术. Docker的etcd ...

  9. 安卓高级4 第三方库SlidingMenu的使用

    源码位于github上(本人fork地址):点击进入地址 效果图: 使用方法:下载源码后 解压其中的文件夹library 到任意地方 修改library中gragle 其方法参考另一个博客(建议先修改 ...

  10. Android开发技巧——大图裁剪

    本篇内容是接上篇<Android开发技巧--定制仿微信图片裁剪控件> 的,先简单介绍对上篇所封装的裁剪控件的使用,再详细说明如何使用它进行大图裁剪,包括对旋转图片的裁剪. 裁剪控件的简单使 ...