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. Good Bye 2015 A

    Problem A:http://codeforces.com/problemset/problem/611/A A. New Year and Days 题意:某人要在2016年收集糖果,有两种不同 ...

  2. 对jQuery.extend()方法的分析

    jQuery.extend方法是我们常用的方法,也是jQuery源码中的基础方法.它的主要作用是:将一个或多个“源对象”合并到一个“目标对象”中,并返回目标对象.它主要有三种表现形式: a.jQuer ...

  3. HDU4756+Prim

    题意简单:去掉最小生成树的某一条边并补上一条,求MaxVal 思路:贪心(借鉴Yamidie的思路...) 分别求出最小生成树和次最小生成树,再在这两棵树上求最小生成树 #include<std ...

  4. 多线程 (五)NSOperation

    NSOperation是对GCD的分装,OC语言,更简单方便 NSOperation和NSOperationQueue一起使用也能实现多线程编程 基本步骤: 将操作封装到一个NSOperation对象 ...

  5. cocos2dx Sprite setBlendFunc 使用颜色混合:加算,减算

    说明: 图片的混色是游戏的特效的特破口,应用的好,可以大量减少图片的使用量,有专门的介绍资料... setBlendFunc  相关参数,有多种组合方式,需要经验的积累 http://blog.csd ...

  6. Android:控件布局(单帧布局)FrameLayout

    FrameLayout:所有控件位于左上角,并且直接覆盖前面的子元素. 在最上方显示的层加上: android:clickable="true" 可以避免点击上层触发底层. 实例: ...

  7. Javascript学习笔记(一)

    1.form表单中button按钮的事件会导致表单的提交,可以改成<input type='button'  /> 实现 2. 手动触发select的onchange事件 var sele ...

  8. 一篇文章教会你,如何做到招聘要求中的“要有扎实的Java基础

    来历 本文来自于一次和群里猿友的交流,具体的情况且听LZ慢慢道来. 一日,LZ在群里发话,“招人啦.” 然某群友曰,“群主,俺想去.” LZ回之,“你年几何?” 群友曰,“两年也.” LZ憾言之,“惜 ...

  9. 【原创】海量数据处理问题(一) ---- 外排,堆排,K查找的应用

    这篇博客源自对一个内存无法处理的词频统计问题的思考,最后给出的解决办法是自己想的,可以肯定这不是最好的解法.但是通过和同学的讨论,仍然感觉这是一个有意义及有意思的问题,所以和大家分享与探讨. 如果有误 ...

  10. Codeforces Round #221 (Div. 2) C. Divisible by Seven(构造 大数除法 )

    上几次的一道cf题. 题目:http://codeforces.com/contest/376/problem/C 性质: (4)a与b的和除以c的余数(a.b两数除以c在没有余数的情况下除外),等于 ...