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 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
if (NULL==p && NULL==q) return true; if (NULL!=p && NULL==q) return false; if (NULL !=q && NULL==p) return false; if (p->val == q-> val)
return (isSameTree(p->left,q->left) && isSameTree(p->right,q->right));
else
return false;
}
};

题目答案

思路:采用递归的方法,先判断(1)p和q都为NULL,则返回true;(2)p和q一个是NULL,一个不是NULL,则返回false;(3)如果(1)和(2)都不是,则证明两个tree都不是NULL,判断p->value是否等于q->value,若不等于,则返回false;若等于,则返回(他们的左子树相等 and 他们的右子树相等)。

[leetcode.com]算法题目 - Same Tree的更多相关文章

  1. [leetcode.com]算法题目 - Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  2. [leetcode.com]算法题目 - Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  3. [leetcode.com]算法题目 - Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. [leetcode.com]算法题目 - Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  5. [leetcode.com]算法题目 - Gray Code

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  6. [leetcode.com]算法题目 - Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. [leetcode.com]算法题目 - Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  8. [leetcode.com]算法题目 - Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. class Solution { public: int sqr ...

  9. [leetcode.com]算法题目 - Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

随机推荐

  1. IOS初级:app的启动图像

    1,准备若干张适合不同设备满屏幕的图像 这里补充说明一下图像的命名方法: iphone4屏幕 Default@2x.png iphone5屏幕 Default-568h@2x.png  (568*2即 ...

  2. Find the location of libmysqlclient.so.X file in Linux environments

    I'm putting together a script that has a requirement of knowing libmysqlclient.so.[15|16|18] .so fil ...

  3. virtual、abstract、interface区别以及用法

    virtual 用于在基类中的使用的方法,使用的情况为:      情况1:在基类中定义了virtual方法,但在派生类中没有重写该虚方法.那么在对派生类实例的调用中,该虚方法使用的是基类定义的方法. ...

  4. 2019.01.22 zoj3583 Simple Path(并查集+枚举)

    传送门 题意简述:给出一张图问不在从sss到ttt所有简单路径上的点数. 思路: 枚举删去每个点然后把整张图用并查集处理一下,同时不跟sss和ttt在同一个连通块的点就是满足要求的点(被删去的不算). ...

  5. 2019.01.21 bzoj3674: 可持久化并查集加强版(主席树+并查集)

    传送门 题意:维护可持久化并查集,支持在某个版本连边,回到某个版本,在某个版本 询问连通性. 思路: 我们用主席树维护并查集fafafa数组,由于要查询历史版本,因此不能够用路径压缩. 可以考虑另外一 ...

  6. 使用Ant发布web应用到tomcat

    使用Ant发布web应用到tomcat 来自:http://blog.csdn.net/hbcui1984/article/details/1954537 今天在公司用ant写了个部署web应用的脚本 ...

  7. C语言的问题,头文件:keil也许有漏洞

    2018-06-15   16:52:03 ------------------------------------------------------------------------------ ...

  8. 音频管理器(AudioManager)

    MainActivity.java package com.wwj.serviceandboardcast;   import android.app.Activity; import android ...

  9. javascript 经典问题汇总

    1. ["1","2","3"].map(parseInt) 为多少?答:[1,NaN,NaN]array.map(function(cur ...

  10. python 基础_列表的其他操作 4

    一.查找某个元素在数组中出现的次数 ,count的运用 a = ['a','b','c','c','c','a'] print(a.count('c')) 二.把一个元素插入到另一个元素的末尾,ext ...