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

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

Example 1:

Input:
/ \ / \ [,,], [,,] Output: true

Example 2:

Input:
/ \ [,], [,null,] Output: false

Example 3:

Input:
/ \ / \ [,,], [,,] Output: false

方法一:使用递归(C++)

 bool isSameTree(TreeNode* p, TreeNode* q) {
if(p==NULL&&q==NULL)
return true;
if((!p&&q)||(p&&!q)||(p->val!=q->val))
return false;
return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
}

LeetCode 100. Same Tree 判断两棵二叉树是否相等 C++的更多相关文章

  1. same tree(判断两颗二叉树是否相等)

    Input: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1,2,3] Output: true Example 2: Input: 1 1 / \ 2 2 [1,2], [1,nul ...

  2. LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

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

  3. [LeetCode]100. Same Tree判断树相同

    dfs遍历一下判断 public boolean isSameTree(TreeNode p, TreeNode q) { if (p==null) { return q == null; } els ...

  4. (二叉树 递归 DFS) leetcode 100. Same Tree

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

  5. Leetcode 100 Same Tree 二叉树

    就是判断两棵树的值和结构是否相同 注意:要判断是否所有的树节点是否为NULL /** * Definition for a binary tree node. * struct TreeNode { ...

  6. 剑指offer17:输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

    1 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 2 思路和方法 (1)先在A中找和B的根节点相同的结点 (2)找到之后遍历对应位置的其他结点, ...

  7. leetcode 100. Same Tree

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

  8. leetcode 100 Same Tree ----- java

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

  9. Invert a binary tree 翻转一棵二叉树

    Invert a binary tree 翻转一棵二叉树 假设有如下一棵二叉树: 4  / \   2    7  / \   / \ 1  3 6  9翻转后: 4     /    \    7 ...

随机推荐

  1. PTA——字符串逆序

    PTA 7-59 字符串逆序 #include<stdio.h> #include<string.h> #define N 81 int main() { int i; cha ...

  2. PowerShell在激活virtualenv虚拟环境时禁止运行的脚本的解决办法

    问题描述 在使用Django开发网站项目时,为了便于修改.维护以及项目部署,使用了virtualenv虚拟环境.这个工具允许你维护多个分离的Python环境,每个都具有它自己的库和包的命名空间.这种情 ...

  3. Linux可以生产uImage

    默认kernel只生产Image和zImage,若想让kernel生产uImage,需要用到mkimage,这个是uboot可以提供的,位于uboot/tool/目录下,将其加入到环境变量即可.

  4. Oracle—通过操作系统进程查看数据库sql语句

    工作中遇到一个问题,某报表运行时间特别长,通过操作系统可以看到一个oracle进程消耗资源比较大,如何能够通过该操作系统进程找到具体SQL呢.记录如下: 1.查看Linux系统进程号 可以通过top动 ...

  5. WMware Vsphere取消某虚机的漂移

    由于一些业务特性,有一些虚机不应该完全受集群DSR控制.下面记录一下如何更改某一虚机的漂移属性. 1.环境 VMware Vsphere web client 6.5 2.点击需要配置虚机所在的集群, ...

  6. 宝塔linux面板 解决TP3.2 404

    在配置文件中加入一下配置: location / { if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1; } } location ...

  7. HI3518EV200+AR0130开发板烧录uboot、kernel、rootfs及其参数配置

    分区名 分区大小 起始地址 截至地址bootloader:1M 0x00000000 0x00100000kernel: 3M 0x00100000 0x00400000rootfs: 12M 0x0 ...

  8. nc--windows下工具分享

    1.在windows下安装了9个memcached. 一些测试需要经常对这9个memcached的执行flush_all的操作 由于windows没有linux那样可以使用nc命令. 经过不懈搜索,找 ...

  9. 利用微软RD Client APP远程连接PC(附外网连接方法)

    一.下载RD Client 这个就不用多说了... 二.设置PC允许远程桌面连接 PC系统以win10为例: 1.进入“远程设置”允许远程协助与远程桌面连接 桌面右键单击“此电脑”,属性,单击左边“远 ...

  10. 工控随笔_18_西门子_WinCC的VBS脚本_07_变量作用域和传值、传址

    在vbs脚本中也存在和其他编程语言一样的概念,那就是变量的作用域,变量的作用域决 定在什么范围内可以访问. 同样的在vbs脚本中对于变量也有一个生命周期, 变量的生命周期决定了变量的存续时间 这个主要 ...