题目

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相关推荐

  1. LeetCode 100. Same Tree

    LeetCode 100. Same Tree Solution1: 这种弱智题提交这么多次... /*** Definition for a binary tree node.* struct Tr ...

  2. [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 ...

  3. [LeetCode]: 100: Same Tree

    题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

  4. leetcode 100.Same Tree

    这道题目我最初想的太多了,导致做了好久,之后从网上一看,一下醒悟过来,不难. 题目: Given two binary trees, write a function to check if they ...

  5. LeetCode 589. N-ary Tree Preorder Traversal-多子节点树前序遍历--递归,迭代--反向压栈--C++解法

    LeetCode 589. N-ary Tree Preorder Traversal-多子节点树前序遍历–递归,迭代–反向压栈–C++解法 LeetCode题解专栏:LeetCode题解 LeetC ...

  6. 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 ...

  7. 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 ...

  8. LeetCode: Flatten Binary Tree to Linked List

    LeetCode: Flatten Binary Tree to Linked List LeetCode: Flatten Binary Tree to Linked List Given a bi ...

  9. 【重点】LeetCode 124. Binary Tree Maximum Path Sum

    LeetCode 124. Binary Tree Maximum Path Sum 参考链接:http://zxi.mytechroad.com/blog/tree/leetcode-124-bin ...

最新文章

  1. Hutool之集合工具——CollectionUtil
  2. jetbrains intellij IDEA 常用插件和配置
  3. OO实现ALV TABLE 十:ALV的页眉页脚
  4. VirtualBox 扩展虚拟硬盘容量
  5. C语言实验源程序保存,实验一 C语言集成开发环境
  6. 前端代码的整洁之道 | 技术头条
  7. 设计模式入门,工厂模式,c++代码实现
  8. vue拦截器刷新登陆页面_vue页面跳转拦截器
  9. 软件网站安全性的设计与检测与解决方案
  10. MySQL(15)-----运算符和优先级查询结果拼接处理及CONCAT()、CONCAT_WAS()和GROUP_CONCAT()函数的使用
  11. 模式识别人工神经网络BP算法
  12. SNIFFER(嗅探器)-简介(zt)
  13. 个人对于封装继承多态的理解
  14. 视频号拍摄技巧和制作方法有哪些?
  15. 启智平台git使用指引
  16. linux驱动系列学习之OLED(i2c接口)(八)
  17. JS入门到入土之数字运算符扩展
  18. 微信变身大史记:从IM到电商 腾讯帝国新时代http://danke74520.qzone.qq.com
  19. H网、某播放器、某数字公司是一根绳上的三个蚂蚱
  20. oracle统计信息内容,oracle搜集统计信息

热门文章

  1. 怎么快速搭建属于自己的博客
  2. 【Xilinx-Petalinux学习】-02-建立PetaLinux工程
  3. NHibernate入门实例
  4. 数据结构---AVL树调整方法(详)
  5. java编程应该要的英语_java编程中常用英语单词
  6. OJ、ACM提交常见错误类型
  7. 2.5D屏幕有什么好处?
  8. AI 是否会取代计算机程序员
  9. 退休后多长时间能领到工资?
  10. 红豆、绿豆、黑豆、花生、莲子、薏仁米放在一起吃,可以吗?