给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

示例 1:

输入:p = [1,2,3], q = [1,2,3]
输出:true

示例 2:

输入:p = [1,2], q = [1,null,2]
输出:false

示例 3:

输入:p = [1,2,1], q = [1,1,2]
输出:false

提示:

  • 两棵树上的节点数目都在范围 [0, 100] 内
  • -104 <= Node.val <= 104

这个问题可以通过二叉树的遍历来完成,二叉树的遍历有4种方式(前序,中序,后序,层次),遍历有迭代,递归2种写法,因此,这个题的解法有8种.

本人选择了前序遍历进行求解,代码如下:

public boolean isSameTree(TreeNode p, TreeNode q) {if (null == p && null == q) {return true;} else if (null != p && null != q) {if (p.val == q.val) {return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);}return false;} else {return false;}}

100.[leetcode]same-tree相关推荐

  1. [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

    目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...

  2. Leetcode | Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  3. LeetCode N-ary Tree Level Order Traversal(bfs)

    问题:给出一个n叉树,输出层次遍历 思路:bfs 具体代码参考: https://github.com/wuli2496/OJ/tree/master/LeetCode/N-ary%20Tree%20 ...

  4. LeetCode Binary Tree Right Side View(搜索)

    问题:给出一个二叉树,要求输出右视图 思路:因为要求输出右视图.可以考虑使用深度优先搜索或者 广度优先搜索. 使用深度优先搜索时,以非递归形式,将左右子树入栈,同时使用哈希表记录深度与对应右视图的值. ...

  5. LeetCode Binary Tree Preorder Traversal(二叉树的前序遍历)

    问题:给出一个二叉树,输出前序遍历 思路: 自顶向下遍历过程中,将当前结点的值加入到list中,然后处理左.右子树 具体代码参考: https://github.com/wuli2496/OJ/tre ...

  6. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  7. [LeetCode] Binary Tree Postorder题解

    Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For ...

  8. [Leetcode] Binary Tree Maximum Path Sum

    这是LeetCode上的一道题目,需要求二叉树中两点路径的最大和.原题是 https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ ...

  9. [LeetCode] N-ary Tree Postorder Traversal N叉树的后序遍历

    Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...

  10. [leetcode] Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...

最新文章

  1. Macbook 中如何通过命令行打开 vscode
  2. 棒棒糖 宏_棒棒糖图表
  3. Vue基础之Vue条件渲染
  4. linq from 多个sum_快手重拳打击劣质电商 7月以来封禁700多个团伙账号
  5. camelcase_在Python中将字符串转换为camelCase
  6. 激怒开源社区,微软悄悄删除2500行功能代码后致歉:已恢复!
  7. Zeusee 开源移动端车型识别系统HyperVID
  8. Node编码格式的设置
  9. 贾跃亭:FF未来主义者们最终所要创造的社会价值 一定会得到资本市场的认可...
  10. 注册 aspnet_regiis
  11. python consulate_使用python测测你的系统最多能创建多少个线程 | 学步园
  12. python 广义线性模型_SPSS广义线性模型:广义估计方程
  13. Pwn-2018_HITB_CTF-gundam
  14. word---尾注法插入参考文献
  15. html 分享页面到微博qq空间
  16. 基于python的微信机器人_基于python的微信机器人
  17. 2021年中国海上风电行业发展现状分析,双碳循环下行业飞速发展「图」
  18. java.sql.SQLException: ORA-00911: 无效字符(编译器大坑)
  19. 如何成功地安装OpenEXR
  20. 如何用老毛桃PE启动盘进入PE系统给硬盘分区

热门文章

  1. C++:布尔类型(bool)详细分析
  2. 三菱FX5U-绝对定位指令 DRVA(DDRVA) 两种写法
  3. 密码算法(SM1、SM2、SM3、SM4、同态加密、密态计算、隐私计算和安全多方计算)
  4. python 将PDF 转成 图片的几种方法
  5. 第6章 放大器的频率特性
  6. Java校验框架使用@Valid、@Validated、OVAL+Groovy
  7. C语言实验题目[01]
  8. 做网站服务器空间,做网站服务器空间
  9. 【CodeChef】【DP】Count Subsequences
  10. 疑似网易泄露用户密码事件浅析