[leetcode]100.Same Tree
题目
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.
解法一
思路
先序遍历的递归
代码
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/
class Solution {public boolean isSameTree(TreeNode p, TreeNode q) {if(p == null && q == null) return true;if(p == null || q == null) return false;if(p.val != q.val)return false;return isSameTree(p.left, q.left)&&isSameTree(p.right, q.right);}
}
解法二
思路
用栈或者队列来实现层次遍历,一个栈(队列) 或者 两个栈(队列)都可以。如果用一个栈(队列),那就是两棵树同一个位置的节点同时入栈,出栈的时候同时出栈,然后进行对比。以下用一个队列实现。
代码
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/
class Solution {public boolean isSameTree(TreeNode p, TreeNode q) {Deque<TreeNode> queue = new LinkedList<>();queue.addLast(p);queue.addLast(q);while(!queue.isEmpty()){p = queue.removeFirst();q = queue.removeFirst();if(p == null && q == null)continue;else if(p == null || q == null || q.val!=p.val)return false;else {queue.addLast(p.left);queue.addLast(q.left);queue.addLast(p.right);queue.addLast(q.right);}}return true;}
}
转载于:https://www.cnblogs.com/shinjia/p/9736638.html
[leetcode]100.Same Tree相关推荐
- LeetCode 100. Same Tree
LeetCode 100. Same Tree Solution1: 这种弱智题提交这么多次... /*** Definition for a binary tree node.* struct Tr ...
- [Leetcode]100. Same Tree -David_Lin
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [LeetCode]: 100: Same Tree
题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
- leetcode 100.Same Tree
这道题目我最初想的太多了,导致做了好久,之后从网上一看,一下醒悟过来,不难. 题目: Given two binary trees, write a function to check if they ...
- LeetCode 589. N-ary Tree Preorder Traversal-多子节点树前序遍历--递归,迭代--反向压栈--C++解法
LeetCode 589. N-ary Tree Preorder Traversal-多子节点树前序遍历–递归,迭代–反向压栈–C++解法 LeetCode题解专栏:LeetCode题解 LeetC ...
- LeetCode: 107. Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode: 103. Binary Tree Zigzag Level Order Traversal
题目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left ...
- LeetCode: Flatten Binary Tree to Linked List
LeetCode: Flatten Binary Tree to Linked List LeetCode: Flatten Binary Tree to Linked List Given a bi ...
- 【重点】LeetCode 124. Binary Tree Maximum Path Sum
LeetCode 124. Binary Tree Maximum Path Sum 参考链接:http://zxi.mytechroad.com/blog/tree/leetcode-124-bin ...
最新文章
- Hutool之集合工具——CollectionUtil
- jetbrains intellij IDEA 常用插件和配置
- OO实现ALV TABLE 十:ALV的页眉页脚
- VirtualBox 扩展虚拟硬盘容量
- C语言实验源程序保存,实验一 C语言集成开发环境
- 前端代码的整洁之道 | 技术头条
- 设计模式入门,工厂模式,c++代码实现
- vue拦截器刷新登陆页面_vue页面跳转拦截器
- 软件网站安全性的设计与检测与解决方案
- MySQL(15)-----运算符和优先级查询结果拼接处理及CONCAT()、CONCAT_WAS()和GROUP_CONCAT()函数的使用
- 模式识别人工神经网络BP算法
- SNIFFER(嗅探器)-简介(zt)
- 个人对于封装继承多态的理解
- 视频号拍摄技巧和制作方法有哪些?
- 启智平台git使用指引
- linux驱动系列学习之OLED(i2c接口)(八)
- JS入门到入土之数字运算符扩展
- 微信变身大史记:从IM到电商 腾讯帝国新时代http://danke74520.qzone.qq.com
- H网、某播放器、某数字公司是一根绳上的三个蚂蚱
- oracle统计信息内容,oracle搜集统计信息