LeetCode 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.
题意:给出两个二叉树,判断这两个树是不是相等
思路:用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相关推荐
- [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)
目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...
- 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. ...
- LeetCode N-ary Tree Level Order Traversal(bfs)
问题:给出一个n叉树,输出层次遍历 思路:bfs 具体代码参考: https://github.com/wuli2496/OJ/tree/master/LeetCode/N-ary%20Tree%20 ...
- LeetCode Binary Tree Right Side View(搜索)
问题:给出一个二叉树,要求输出右视图 思路:因为要求输出右视图.可以考虑使用深度优先搜索或者 广度优先搜索. 使用深度优先搜索时,以非递归形式,将左右子树入栈,同时使用哈希表记录深度与对应右视图的值. ...
- LeetCode Binary Tree Preorder Traversal(二叉树的前序遍历)
问题:给出一个二叉树,输出前序遍历 思路: 自顶向下遍历过程中,将当前结点的值加入到list中,然后处理左.右子树 具体代码参考: https://github.com/wuli2496/OJ/tre ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] Binary Tree Postorder题解
Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For ...
- [Leetcode] Binary Tree Maximum Path Sum
这是LeetCode上的一道题目,需要求二叉树中两点路径的最大和.原题是 https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ ...
- [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 ...
- [leetcode] Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
最新文章
- 【博士论文】物联网数据安全可信的共享技术研究
- 实用的bit 位操作
- JVM调优实战:G1中的to-space exhausted问题
- C#操作xml文件:使用XmlDocument 实现读取和写入
- uvali5697(DP)
- Parcel是个好玩意儿
- html绝对定位怎么页面居中,html – 如何将绝对定位的元素居中?
- node.js中对 redis 的安装和基本操作
- js获取自定义属性的值
- 入门系列之使用fail2ban防御SSH服务器的暴力破解攻击 1
- virtio-blk浅析[转]
- struts教程笔记1
- 打开WORD 2003时提示发现错误,需要关闭,还要发送错误报告给Microsoft 解决方案...
- 群晖硬盘已损毁 Linux 修复,今天群晖存储空间损毁,起死回生
- Python PIL和二进制图片互转
- Mybatis-Plus入门案例以及使用方法
- 三年通过注册会计师考试的经历及心得
- 文档处理容易“翻车”,来看看CCIG上的大咖怎么说
- 设置图片的宽高一样大
- 双生世界服务器维护,我的世界宝可梦彼岸双生服务器
热门文章
- [JS]视频总结-第三部分_深入javascript
- C#统计一段时间内有多少个星期几
- 【转】基本概念:过拟合、修剪、假正、假负
- 如何对SQL Server数据库中的孤立用户和系统及用户建立映射
- Android模拟器入门
- SQL Server 存储过程的分页
- canal能监控多个mysql_learning-mysql-canal
- jsonproperty注解_Jackson注解详解
- python安装在什么系统下最好-学python语言用什么软件比较好?需要安装哪些软件?...
- 查看电脑python虚拟环境-手把手教你在Linux系统下使用Python虚拟环境