/** * 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; } LinkedList<TreeNode> queue1 = new LinkedList<TreeNode>(); queue1.add(p); LinkedList<TreeNode> queue2 = new LinkedList<TreeNode>(); queue2.add(q); while (!queue1.isEmpty() && !queue2.isEmpty()){ TreeNode node1 = queue1.remove(); TreeNode node2 = queue2.remove(); if(node1.val != node2.val){ return false; } if(node1.left!=null){ queue1.add(node1.left); } if(node2.left != null){ queue2.add(node2.left); } if(queue1.size() != queue2.size()){ return false; } if(node1.right!=null){ queue1.add(node1.right); } if(node2.right!=null){ queue2.add(node2.right); } if(queue1.size() != queue2.size()){ return false; } } return true; } }
相关推荐
public void helper(TreeNode root, int level){// 当前层没有 list,新建// 取得当前层的 list迭代pub
java lru leetcode leetcode-algorithms-java leetcode 算法笔记-java
leetcode-tag-Tree
java lru leetcode LeetCode-Tag-Java 解决方案 LeetCode 的解决方案 指数
力扣解题java版本
https://github.com/geekxingyun/leetcode-java-algorithm-solution.git 然后你会发现当前项目有两个模块。 完成:它是完成的项目,并有一些关于 leetcode 的答案。 初始:只需在模块项目中编写代码 如果您不知道...
leetcode Leetcode-Java-解决方案 Java 中 Leetcode 问题的解决方案
用Java实现基础数据结构,排序算法、经典算法以及leetcode刷题记录_Java_下载.zip
LeetCode-Python-Java zhouzhenping/LeetCode-Python&Java 参考代码来源 力扣(LeetCode) 链接: 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 Github 01 两数之和 给定一个整数数组 ...
#Leetcode-In-Java 代码并不全是本人写的,有的参考了网络上其他前辈的想法,但都能在OJ上AC。 ###索引 1 . Two-Sum 要点: - 利用java中Array对象的sort方法排序,使得整个数组呈升序状态 - 再利用两段取点...
leetcode 答案LeetCode-答案-Java 我的 leetcode 用伪代码和分析用 java 回答
分类LeetCode-Java-接受 这是 Leetcode 问题的 Java 解决方案。 细节 标题和答案格式 /* * 17. Letter Combinations of a Phone Number * Target: Given a string containing digits from 2-9 inclusive, return all...
然后通过用循环来解:假设第一个for循环是一个数组的循环,而后它的内嵌循环是也是这个数组,只是下标从0变成了1,这样,在第一次循环时,第1个元素会与其他所有元素
LeetCode-Practice-Java leetcode 练习题 这个项目就是自己练习leetCode的实际Demo 有些公司就会问一些算法问题,即便是不问的话自己不学这块感觉也对不起自己的职业。。。 所以自己在学习的时候 也打算把优秀的答案...
LeetCode题解 - Java语言实现-181页.pdf
leetcode-for-Java Record the process of leetcode for Java. 引言 记录一下,在leetcode上刷题的过程。 前期以刷题为主。有空的话就更新一下这个目录,然后顺带把有写问题的解题思路写上。 目录
leetcode 答案Leetcode-Java - 符合条件(Hacktoberfest) JAVA 中的 Leetcode 答案
leetcode-cli-plugins leetcode-cli 的第 3 方插件。 什么是 如何使用 如何使用 插件 名称 描述 增强的命令 按公司或标签过滤问题 list 不要在同一台计算机上使 Chrome 的会话过期 login 不要在同一台计算机上使 ...
Leetcode-Java- 一.(Sort类): 350. Intersection of Two Arrays II a.首先用HashMap遍历一遍数组nums1,Key值储存数组元素,Value(初始值为1)值储存重复元素出现次数,每出现一次加1; b.用List储存nums2中与nums...
leetcode 答案Leetcode---数据库 我对 Leetcode 数据库问题的回答