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. fdsafdsafds

    name1 = input('请输入一个名字:') name2 = input('请输入一个名字: ') animal = input('请输入一个名字 :') print('刚按完摩的{}出门看见{ ...

  2. 2018.5.3 maven

     1  maven基本概念  1.1maven是什么 1)软件项目管理和理解工具      2)项目对象模型(Project Object Model,POM)      3)项目的构建.报告和文档的 ...

  3. Mac上,Apache启动正常,却无法访问localhost和127.0.0.1

    mac系统,之前一直好好的,今天突然localhost以及127就突然打不开了.显示拒绝访问. 各种方法都试过了,不是端口占用, 不是日志文件缺失,任何情况都不是. 想了想,之前有升级过PHP从5升级 ...

  4. Linux 系统的用户和组

    目录 1. 用户及组相关文件 2. 用户相关查询 2.1 直接通过cat文件查看用户及组文件内容 2.2 使用下面查询命令查看 3. 使用操作命令修改用户及组相关文件 3.1 专有编辑命令(仅限高级用 ...

  5. 用python来自动玩类似跳一跳的小游戏

    最近春节,qq上出了一个叫穿越福城的小游戏.游戏的玩法类似挑一挑,也是通过一个个木桩.只不过把跳的过程变成了搭梯子.按的时间越长,梯子越长.梯子过长或者过短小企鹅都会掉下去,游戏失败.我的目的是用py ...

  6. 批量找注入 python3+sqlmap结合

    注入一直都是用sqlmap  导致本来就不怎么精通的手工注入现在就忘的一干二净 想实战练习  却一时又找不到有注入的网站   于是便有了这篇文章 想找个批量获取域名链接的工具   但都是只是获取域名而 ...

  7. web方向编程语言最全对比

    web方向编程语言最全对比 目前一般公司的后台用的开发语言大概有以下几种:java,python,php,asp.net,c++,node.js,ruby on rails 等. java 优点:性能 ...

  8. 【转载】win10解决设置默认打开方式不生效问题(双击每次都要选择默认打开程序)

    win10解决设置默认打开方式不生效问题(双击每次都要选择默认打开程序) 以下文章 部分选自 https://blog.csdn.net/shan165310175/article/details/8 ...

  9. webbrowser 里的js函数和C#的函数互相调用方式

    1.c#程序里要添加  [System.Runtime.InteropServices.ComVisibleAttribute(true)] 和  webBrowser1.ObjectForScrip ...

  10. 精读《C++ primer》学习笔记(第一至三章)

    第一章: 重要知识点: 类型:一种类型不仅定义了数据元素的内容,还定义了这类数据上可以进行的运算:所以说类定义,实际上就是定义了一种数据类型: >>和<<运算符返回其左侧的运算 ...