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. laravel 使用 php artisan make:model到指定目录(controller同理)

    在 \app\Models 目录下创建一个BusinessProduct模型文件 D:\htdocs\PHPTutorial\WWW\gf>php artisan make:model /Mod ...

  2. SpringBoot配置(2) slf4j&logback

    SpringBoot配置(2) slf4j&logback 一.SpringBoot的日志使用 全局常规设置(格式.路径.级别) SpringBoot能自动适配所有的日志,而且底层使用slf4 ...

  3. 论气机之"左升右降"

      生命现象源于气机的出入升降运动. “出入废则神机化灭,升降息则气立孤危.故非出入,则无以生长壮老已:非升降,则无以生长化收藏”(<素问·六微旨大论>),升降是气机主要的运动形式之一,是 ...

  4. shell脚本(二)

              shell脚本(二)——变量 一.定义:用来存放各种数据,编程语言组成部分 变量的命名规则: 变量名由数字 字母下划线组成 必须以字母或者下划线开头 不能使用shell里面的关键词 ...

  5. 【函数】isinstance内建函数(小窗help)

    #学到了第八天,还有很多没有理解,不过,没关系,相信任何复杂的问题都是由简单的组成,只有将每一个细节理解到位,自然问题迎刃而解 今天遇到了isinstace函数,忘了,先看一下语法 查百度附上链接:h ...

  6. 1.微信小程序里如何设置当地时间?

    方法一: 1.效果图 2.wxml code: <!--pages/index/index.wxml--> <text>当前时间:{{time}}</text> & ...

  7. ActiveMQ(1)---初识ActiveMQ

    消息中间件的初步认识 什么是消息中间件? 消息中间件是值利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排队模型,可以在分布式架构下扩展进 ...

  8. spring学习1

    1.<context:property-placeholder/> :用于从外部属性文件中获取Bean的配置 <context:property-placeholder locati ...

  9. Rabbitmq(6) 主题模式

    * 匹配1个 # 匹配所有 发送者: package com.aynu.bootamqp.service; import com.aynu.bootamqp.commons.utils.Amqp; i ...

  10. 深度学习原理与框架-猫狗图像识别-卷积神经网络(代码) 1.cv2.resize(图片压缩) 2..get_shape()[1:4].num_elements(获得最后三维度之和) 3.saver.save(训练参数的保存) 4.tf.train.import_meta_graph(加载模型结构) 5.saver.restore(训练参数载入)

    1.cv2.resize(image, (image_size, image_size), 0, 0, cv2.INTER_LINEAR) 参数说明:image表示输入图片,image_size表示变 ...