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.

Example 1:

Input:     1         1
/ \ / \
2 3 2 3 [1,2,3], [1,2,3] Output: true

Example 2:

Input:     1         1
/ \
2 2 [1,2], [1,null,2] Output: false

题意:

给定两个树,判断两个数是否相同

Solution1:  DFS  (Code will be neat and simple)

code

 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 isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
return false;
}
}

Solution2:  Iteration(Using a stack or a queue to simulate how DFS works)

code

 class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
Queue<TreeNode> queue = new LinkedList<>(); queue.add(p);
queue.add(q); while(!queue.isEmpty()){
TreeNode node1 = queue.remove();
TreeNode node2 = queue.remove(); if(node1==null && node2 ==null) continue;
if(node1==null || node2==null) return false;
if(node1.val != node2.val) return false; queue.add(node1.left);
queue.add(node2.left); queue.add(node1.right);
queue.add(node2.right);
}
return true;
}
}

[leetcode]100. Same Tree相同的树的更多相关文章

  1. LeetCode 100. Same Tree (判断树是否完全相同)

    100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...

  2. LeetCode 100. Same Tree相同的树 (C++)

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

  3. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

  4. [LeetCode] 100. Same Tree 相同树

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  5. LeetCode 100. Same Tree (相同的树)

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

  6. [LeetCode]100. Same Tree判断树相同

    dfs遍历一下判断 public boolean isSameTree(TreeNode p, TreeNode q) { if (p==null) { return q == null; } els ...

  7. 【LeetCode】Same Tree(相同的树)

    这道题是LeetCode里的第100道题. 这是题目: 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 ...

  8. [LeetCode] Graph Valid Tree 图验证树

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  9. LeetCode 101. Symmetric Tree (对称树)

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

随机推荐

  1. cookie mapping 原理理解

    深入浅出理解 COOKIE MAPPING Cookie mapping技术 利用javascript跨域访问cookie之广告推广

  2. Javascript 来判断数组的假值如 null false "" NaN

    Javascript 来判断数组的假值如 null false "" NaN function bouncer(arr) { arr = arr.filter(function(a ...

  3. insert 插入

    自动关联当前时间: GETDATE():返回当前时间和日期.

  4. jquery,attr,prop,checkbox标签已有checked=checked但是不显示勾选

    最近在做项目的过程中碰到了这样的一个问题:在使用bootstrap模态框的过程中,在模态框中有一个checkbox标签,一开始是为选中的,当点击触发模态框按钮,选中chcekbox时,会显示勾选,这个 ...

  5. Flume调优

    这是一个关于池子的故事.有一个池子,它一头进水,另一头出水,进水口可以配置各种管子,出水口也可以配置各种管子,可以有多个进水口.多个出水口.水术语称为Event,进水口术语称为Source.出水口术语 ...

  6. 平均数_中位数_众数在SqlServer实现

    平均数.中位数.众数都是度量一组数据集中趋势的统计量.所谓集中趋势是指一组数据向某一中心值靠拢的倾向,测度集中趋势就是寻找数据一般水平的代表值或中心值.而这三个特征数又各有特点,能够从不同的角度提供信 ...

  7. 【java】static用法

    static作用: 用来修饰函数成员,成员变量和成员函数.类对象的属性都一致且能共享,比如国籍,这就能用static修饰,name不能共享,因为每个人都有自己的名字. 特有内容(name)随着对象存储 ...

  8. Delphi调用大漠插件示例

    Delphi XE2 版本调用大漠插件方法:打开Component->Import Component->默认Import a Type Library,点击Next->找到Dm.d ...

  9. mysql优化-数据库优化、SQL优化

    我有一张表w1000,里面有1000万条数据,这张表结构如下:CREATE TABLE `w1000` ( `id` varchar(36) NOT NULL, `name` varchar(10) ...

  10. MySQL导出数据字典

    平时用mysql比较多,有时候需要详细的数据库设计表结构和数据字典,但又没有最新的文档,这个时候直接从数据导出是最新最全的.在MySQL数据库中利用information_schema库中的COLUM ...