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. LocalStorage 本地存储

    首先自然是检测浏览器是否支持本地存储.在HTML5中,本地存储是一个window的属性,包括localStorage和sessionStorage,从名字应该可以很清楚的辨认二者的区别,前者是一直存在 ...

  2. Hibernate注解方法使用总结

    1.类级别注解 @Entity     映射实体类 @Table    映射数句库表 @Entity(name="tableName") - 必须,注解将一个类声明为一个实体bea ...

  3. 团体程序设计天梯赛-练习集L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

  4. Good Bye 2015B

    Problem B:http://codeforces.com/contest/611/problem/B B. New Year and Old Property 题意:问输入的年份a到b中转化为二 ...

  5. call && jmp 指令

    对于jmp指令: (1)jmp short 标号相当于(ip)=(ip)+8位位移 跳转范围是[-128,127](2)jmp near ptr 标号相当于(ip)=(ip)+16位位移 跳转范围是[ ...

  6. Top命令内存占用剖析

    原文: http://yalung929.blog.163.com/blog/static/203898225201212981731971/ 引 言: top命令作为Linux下最常用的性能分析工具 ...

  7. [杂题]FZU2190 非提的救赎

    中文题,题意不多说. 本来感觉很像dp 其实只要从上到下维护单调性就好了 坑是......这个oj......用cin很容易TLE...... //#include <bits/stdc++.h ...

  8. DELPHI下API简述(1800个API)

    DELPHI下API简述 http://zero.cnbct.org/show.asp?id=144 auxGetDevCaps API 获取附属设备容量 auxGetNumDevs API 返回附属 ...

  9. Android 使用split函数进行多个空格分割

    在项目中经常会遇到按字符分割字符串的情况,可以使用String对象的split函数进行分割. 先看实际情况: String str = "关键词1 关键词2 关键词3"; Stri ...

  10. xml格式化

    Vim怎么格式化xml,完全不会,vim的缩进也搞不明白