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. db2用户权限

    前言:     DB2数据库权限分为实例级权限(SYSADM.SYSCTRL.SYSMAINT.SYSMON)和DB2数据库级权限(DBAMD.LOAD).DB2中用户所拥有的权限主要考虑三个方面:实 ...

  2. NOIP2017提高组预赛详解

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

  3. centos7修改root根目录

    1.拷贝/root 原目录的东西到新目录中(包括.xxx文件) /abc 2.修改配置文件 vi /etc/passwd root:x:0:0:root:/root:/bin/bash ==> ...

  4. Hadoop3集群搭建之——虚拟机安装

    现在做的项目是个大数据报表系统,刚开始的时候,负责做Java方面的接口(项目前端为独立的Java web 系统,后端也是Java web的系统,前后端系统通过接口传输数据),后来领导觉得大家需要多元化 ...

  5. STL六大组件

    1.容器 顺序容器.关联容器 2.算法 各种常用算法,sort.search.copy…… 3.迭代器 用来索引容器中元素,是容器与算法之间的胶合剂 4.仿函数(另名函数对象) 仿函数就是让一个类的使 ...

  6. 图片轮滚形式A

    思想解读: 一共有5幅图片,初始时只显示一幅,其余四幅隐藏.然后使用trigger设置右下角的点击事件,根据点击的索引显示某幅图片.原理不算复杂. 结论: trigger的使用,模拟点击 层淡入淡出函 ...

  7. 转:【衬线字体与无衬线字体】font-family之Serif和Sans-Serif

    CSS的font-family(字体系列)的值有许多,如Arial,Georgia,宋体,幼圆等. 一般可以分为两种,衬线字体(serif)和无衬线字体(sans-serif). 如果字体的笔画有末端 ...

  8. JAVA-部署-摘

    一.下载 J2SDK下载http://java.sun.com/j2se/1.4.2/download.html  下载版本是j2sdk-1_4_2_08 ECLIPSE下载http://www.ec ...

  9. java基础-day19

    第08天 异常 今日内容介绍 u  异常体系&异常处理 u  Throwable常用方法&自定义异常 u  递归 第1章   异常产生&异常处理 1.1      异常概述 什 ...

  10. html5打开摄像头并用canvas模拟拍照 - 转

    <video id="video" width="640" height="480" autoplay></video&g ...