100. Same Tree

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 a binary tree node.
* 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) {
if(!p && !q)
{
return true;
}
if(p && q && p->val == q->val)
{
if(isSameTree(p->left, q->left))
{
if(isSameTree(p->right, q->right))
{
return true;
}
}
}
return false;
}
};

leetcode 100的更多相关文章

  1. LeetCode 100. 相同的树(Same Tree) 2

    100. 相同的树 100. Same Tree 题目描述 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 每日一算法2019/5 ...

  2. LeetCode 100. Same Tree (相同的树)

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  3. LeetCode(100)题解--Same Tree

    https://leetcode.com/problems/same-tree/ 题目: Given two binary trees, write a function to check if th ...

  4. LeetCode 100 及 101题

    100. 相同的树 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [ ...

  5. LeetCode 100. Same Tree (判断树是否完全相同)

    100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...

  6. [LeetCode] 100. Same Tree 相同树

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  7. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

  8. Java实现 LeetCode 100 相同的树

    100. 相同的树 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [ ...

  9. leetcode 100. Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

随机推荐

  1. [物理学与PDEs]第4章 反应流体力学

    [物理学与PDEs]第4章第1节 引言 [物理学与PDEs]第4章第2节 反应流体力学方程组 2.1 粘性热传导反应流体力学方程组 [物理学与PDEs]第4章第2节 反应流体力学方程组 2.2 反应流 ...

  2. ERWin & ERStudio图里的实线和虚线的含义[转]

    注: ERWin 与 ERStudio 中这一点的描述方法是一样的. ERWin里面线代表实体间的三种关系:决定关系(Identifying Relationship),非决定关系(None-Iden ...

  3. tcpdump命令--实用篇

    //查看本机与mysql的操作命令 注意 -i any表示监听所有网络接口,我们也根据自身情况选择网络接口 #tcpdump -i any -w - dst port 3306 |strings // ...

  4. Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义(转载)

    From:http://dadekey.blog.51cto.com/107327/119938 我们先写一个简单的脚本,执行以后再解释各个变量的意义   # touch variable # vi ...

  5. DIY--主板跳线接法

    如下图:

  6. spring项目中使用weblogic的连接池

    1.首先在weblogic控制台中配置好一个数据源 我这里建立的数据源的名称叫 jdbc/app1,JNDI名称也叫 jdbc/app1 2.在spring中配置数据源的时候,做如下配置: <b ...

  7. IOS学习之路--BLOCK

    /* 1.定义block变量: 返回值类型 (^block变量名) (参数类型1, 参数类型2, ....); 2.给block变量赋值 block变量名 = ^(参数类型1 参数名称1, ..... ...

  8. zabbix通过sendmail进行邮箱警报

    安装sendmail /usr/lib/zabbix/alertscripts/SendEmail.sh #!/bin/bash to_email_address="$1" # 收 ...

  9. ubuntu14.04开启crontab日志

    ubuntu默认没有开启cron日志记录 1. 修改rsyslog sudo vim /etc/rsyslog.d/50-default.conf cron.* /var/log/cron.log # ...

  10. dubbo服务框架学习

    ====================================================================================== 1.提供注册服务.消费者可 ...