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. mybatis 操作数据错误Truncated incorrect DOUBLE value: ''

    网上查到遇到次错误造成的原因: UPDATE TSYS_ROLE_RIGHTSET ACTIVE_FLAG = '2' and UPDATE_PERSON = 'CaiYiHua' and  UPDA ...

  2. iframe高度自适应实现方案

    iframe高度动态自适应,一直是个头疼的问题,今天我们从事件监听这个角度,来实现iframe高度实时更新. 方案一:监听iframe体的点击事件 <iframe src="casca ...

  3. VB.NET中的DLL编写和调用的最简单示例

    DLL(动态链接库)是一个很有用的东西,在开发大项目的时候显得非常重要,因为多人合作开发时,可以给每个人分配一个任务,用DLL完成,最后组合起来,就不会出现互相冲突的问题.这里给出最简单的DLL编写与 ...

  4. NOIP2017提高组预赛详解

    NOIP2017预赛终于结束了. 普遍反映今年的卷子难度较大,但事实上是这样吗?马上我将为您详细地分析这张试卷,这样你就能知道到底难不难. 对了答案,鄙人考得还是太差了,只有91分. 那么下面我们就一 ...

  5. python学习 day18 (3月25日)---( 面向对象浅析)

    面向对象思想: 字典表示对象: 不是太好 因为 变量 得一个个的赋值改值 {'name':'alex','blood':20,'attack':1,'sex':'不'} {'name':'太亮','b ...

  6. 2018.06.26 NOIP模拟 纪念碑(线段树+扫描线)

    题解: 题目背景 SOURCE:NOIP2015−GDZSJNZXSOURCE:NOIP2015-GDZSJNZXSOURCE:NOIP2015−GDZSJNZX(难) 题目描述 2034203420 ...

  7. NETCLR via C#第四版(文摘)

    第1部分 CLR基础 第1章 CLR的执行模型 第2章 生成,打包,部署和管理应用程序及类型 第3章 共享程序集和强命名程序集 第2部分 设计类型 第4章 类型 第5章 基元类型 引用类型和值类型 第 ...

  8. react组件父传子

    react组件父传子,子组件使用父组件的数据,用props import React, { Component } from 'react'; class App extends Component ...

  9. IntelliJ IDEA 2017版 spring-boot2.0.2 搭建 JPA springboot DataSource JPA环境搭建,JPA实现非字符型设置长度

    1.在github上已有配置环境,如下链接,(需要环境JDK1.8及以上版本,Tomcat1.8及以上版本,搭建maven库,使用编译器IntellJ IDEA) https://github.com ...

  10. 小论文matlab作图技巧

    小论文matlab作图技巧 编辑->复制选项 编辑->图形属性 图中右击->字型 编辑->复制图片,即可. 效果: 宽:5.9高: 7.91