1.题目描述

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.

2.解法分析

与上一篇文章“Symmetric Tree”一样的思路,只是要证明两棵树完全一样,需对两棵树进行一模一样的遍历。当然最好是深度搜索。

/**

 * Definition for binary tree

 * 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) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        vector<TreeNode *> vp;

        vector<TreeNode *> vq;

        

        TreeNode *curp= p;

        TreeNode *curq =q;

        

        while(curp||!vp.empty())

        {

            while(curp)

            {

                if(!curq)return false;

                if(curp->val!=curq->val)return false;

                vp.push_back(curp);

                vq.push_back(curq);

                curp=curp->left;

                curq=curq->left;

            }

            

            if(!vp.empty())

            {

                if(curp)return false;

                curp=vp.back();vp.pop_back();curp=curp->right;

                curq=vq.back();vq.pop_back();curq=curq->right;

            }

        }

        

        if((vp.empty()&&!vq.empty())||(!vp.empty()&&vq.empty()))return false;

        if((curp&&!curp)||(!curp&&curq))return false;

        return true;

        

    }

};

leetcode—Same Tree的更多相关文章

  1. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  2. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  3. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  4. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  5. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  6. [LeetCode] Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  7. [LeetCode] Binary Tree Upside Down 二叉树的上下颠倒

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

  8. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  9. [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  10. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

随机推荐

  1. AForm

    相信大部分程序员都接触过表单,表单是收集用户输入的不二之选,但是表单的开发又是最繁琐.最复杂的,简单地说,开发表单你需要涉及到很多知识: 布局,表单如何布局排版,看起来最清晰整洁,且符合用户体验 控件 ...

  2. httpclient 302 重定向

    主要是由于页面可能不是通过 request.sendRedirect跳转的,可能通过js  location跳转的.所以需要拿跳转后的 地址,重新发送请求...如下所示 if (status == H ...

  3. Unity3D接入移动MM支付SDK(强联网)的问题

    原地址:http://blog.csdn.net/lihandsome/article/details/11919113 因为移动MM支付的SDK只提供android版本的,要自己写过一个androi ...

  4. android下调试unity3d应用

    原地址:http://blog.csdn.net/armoonwei/article/details/7032455 目前貌似不支持断点调试,但可以通过日志打印(logcat)来跟踪. 在androi ...

  5. JavaWeb学习总结(四十九)——简单模拟Sping MVC

    在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...

  6. 如何将sql server数据库转化成sqlite数据库

    今天在将sql server转化为sqlite的数据库的时候,遇到不少的问题,在网上搜了很长时间,都没有找到合适的软件将sql server转化成sqlite,其中用到了SqliteDev软件,在转化 ...

  7. *IntelliJ IDEA配置Hibernate

    为IntelliJ IDEA安装Hibernate插件

  8. 218. The Skyline Problem

    题目: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city ...

  9. CentOS7安装mysql-server

    安装ossec时需要使用到mysql-server,直接安装报错: [root@ossec-server ~]# yum install mysql-server Loaded plugins: fa ...

  10. 《RabbitMQ in action》

    Producers create messages and publish (send) them to a broker server (RabbitMQ).What’s a message? A ...