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. php中at @符号的作用使用说明

    一次,下载别人的源码来看,看到无数@记号,开始以为是注释:后来发现@后面的语句也是会执行的.纳闷了,这个记号究竟是做什么的呢..... 随着学习的不断深入,总算是明白了.这个记号的作用有点类似于asp ...

  2. MySQL利用Navicat导出数据字典

    这里算是一个小技巧 利用mysql的information_schema中的COLUMNS表 和navicat中的导出功能实现快速导出数据字典 CREATE TEMPORARYTABLE `COLUM ...

  3. tcpdump命令--实用篇

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

  4. StringIO 模块用于在内存缓冲区中读写数据

    模块是用类编写的,只有一个StringIO类,所以它的可用方法都在类中.此类中的大部分函数都与对文件的操作方法类似. 例: #coding=gbk import StringIO s=StringIO ...

  5. Windows下Nginx的启动、停止等命令(转)

    Windows下Nginx的启动.停止等命令 在Windows下使用Nginx,我们需要掌握一些基本的操作命令,比如:启动.停止Nginx服务,重新载入Nginx等,下面我就进行一些简单的介绍.1.启 ...

  6. bat文件创建mysql数据库 数据库名为meter

    -- 详见附件,已自测通过      //修改为mysql安装路径     C:\Program Files\MySQL\MySQL Server 5.5"      createtestd ...

  7. IDEA的查询引用、调用关系图的功能

    Eclipse的"Call Hierarchy"可以查看一个Java方法或类成员变量的调用树(caller和callee两个方向),非常方便. 在IDEA中类似功能被划分到了三个命 ...

  8. flask test_client设置cookies

    class TestCase(unittest.TestCase): session = None def setUp(self): self.app = create_app() self.app. ...

  9. C Primer Plus(第五版)6

    第 6 章 C 控制语句 : 循环 在本章中你将学习下列内容 已经多次学过,没怎么标注 · 关键字: for while do while · 运算符: < > >= <= ! ...

  10. Codeforces 450D Jzzhu and Cities [heap优化dij]

    #include<bits/stdc++.h> #define MAXN 100050 #define MAXM 900000 using namespace std; struct st ...