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.

题意:给出两个二叉树,判断这两个树是不是相等

思路:用DFS,首先比较当前结点的值是不是相等,然后比较左子树,再比较右子树

/*** Definition for binary tree* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
public class Solution {public boolean isSameTree(TreeNode p, TreeNode q) {if (p == null && q == null) return true;else if (p == null && q != null) return false;else if (p != null && q == null) return false;else {if (p.val != q.val) return false;if (!isSameTree(p.left, q.left) || !isSameTree(p.right, q.right)) return false;return true;}}
}

第二种方法:用非递归方法,模拟递归的出栈、入栈

/*** Definition for binary tree* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
public class Solution {public boolean isSameTree(TreeNode p, TreeNode q) {Queue<TreeNode> q1 = new LinkedList<TreeNode>();Queue<TreeNode> q2 = new LinkedList<TreeNode>();q1.add(p);q2.add(q);while (!q1.isEmpty() && !q2.isEmpty()) {TreeNode node1 = q1.poll();TreeNode node2 = q2.poll();if (node1 == null && node2 == null) continue;if (node1 == null || node2 == null) return false;if (node1.val != node2.val) return false;q1.add(node1.left); q2.add(node2.left);q1.add(node1.right); q2.add(node2.right);}return true;}
}

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. 【博士论文】物联网数据安全可信的共享技术研究
  2. 实用的bit 位操作
  3. JVM调优实战:G1中的to-space exhausted问题
  4. C#操作xml文件:使用XmlDocument 实现读取和写入
  5. uvali5697(DP)
  6. Parcel是个好玩意儿
  7. html绝对定位怎么页面居中,html – 如何将绝对定位的元素居中?
  8. node.js中对 redis 的安装和基本操作
  9. js获取自定义属性的值
  10. 入门系列之使用fail2ban防御SSH服务器的暴力破解攻击 1
  11. virtio-blk浅析[转]
  12. struts教程笔记1
  13. 打开WORD 2003时提示发现错误,需要关闭,还要发送错误报告给Microsoft 解决方案...
  14. 群晖硬盘已损毁 Linux 修复,今天群晖存储空间损毁,起死回生
  15. Python PIL和二进制图片互转
  16. Mybatis-Plus入门案例以及使用方法
  17. 三年通过注册会计师考试的经历及心得
  18. 文档处理容易“翻车”,来看看CCIG上的大咖怎么说
  19. 设置图片的宽高一样大
  20. 双生世界服务器维护,我的世界宝可梦彼岸双生服务器

热门文章

  1. [JS]视频总结-第三部分_深入javascript
  2. C#统计一段时间内有多少个星期几
  3. 【转】基本概念:过拟合、修剪、假正、假负
  4. 如何对SQL Server数据库中的孤立用户和系统及用户建立映射
  5. Android模拟器入门
  6. SQL Server 存储过程的分页
  7. canal能监控多个mysql_learning-mysql-canal
  8. jsonproperty注解_Jackson注解详解
  9. python安装在什么系统下最好-学python语言用什么软件比较好?需要安装哪些软件?...
  10. 查看电脑python虚拟环境-手把手教你在Linux系统下使用Python虚拟环境