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

Two binary trees are considered equal 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; }
* }
*/

我们最容易想到的,是用栈来实现:

 import java.util.*;
public class Solution {
static Stack<TreeNode> stack1=new Stack<TreeNode>();
static Stack<TreeNode> stack2=new Stack<TreeNode>();
public void toStack(TreeNode p,TreeNode q)
{
while(p.left!=null)
{stack1.push(p);
p=p.left; }
while(q.left!=null)
{stack2.push(q);
q=q.left; }
stack1.push(p);
stack2.push(q); }
public boolean isSameTree(TreeNode p, TreeNode q) {
if((p==null&&q==null))
return true;
if(p==null||q==null)
return false;
while(!stack1.isEmpty())
stack1.pop();
while(!stack2.isEmpty())
stack2.pop();
TreeNode a,b;
toStack(p,q);
while(!stack1.isEmpty()&&!stack2.isEmpty())
{
a=stack1.pop();
b=stack2.pop();
if(a.val!=b.val)//判断该节点的值是否相等
return false; if((a.right!=null)&&(b.right!=null))//都有右孩子,让他们入栈
toStack(a.right,b.right); else if((a.right==null&&b.right!=null)||a.right!=null&&b.right==null)//有一个有右孩子,另外一个没有右孩子
return false;
} if(stack1.isEmpty()&&stack2.isEmpty())
return true;
return false;
} }

当然,还有更简单的,用递归搞定:

 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;
}

更新:

上面两行

     if(p == null && q == null) return true;
if(p == null || q == null) return false;

可以用一行代替:

if (p == NULL || q == NULL) return (p == q);

100. Same Tree(leetcode)的更多相关文章

  1. 100. Same Tree(C++)

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

  2. Device Tree(三):代码分析【转】

    转自:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html Device Tree(三):代码分析 作者:linuxer 发布于:201 ...

  3. 第15个算法-实现 Trie (前缀树)(LeetCode)

    解法代码来源 :https://blog.csdn.net/whdAlive/article/details/81084793 算法来源:力扣(LeetCode)链接:https://leetcode ...

  4. 力扣(LeetCode)删除排序链表中的重复元素II 个人题解

    给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 思路和上一题类似(参考 力扣(LeetCode)删除排序链表中的重复元素 个人题解)) 只不过这里需要用到一个前 ...

  5. easyUI之Tree(树)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  6. 2021.07.19 BZOJ2654 tree(生成树)

    2021.07.19 BZOJ2654 tree(生成树) tree - 黑暗爆炸 2654 - Virtual Judge (vjudge.net) 重点: 1.生成树的本质 2.二分 题意: 有一 ...

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

  8. 第五周 Leetcode 99. Recover Binary Search Tree (HARD)

    Leetcode99 给定一个 二叉搜索树,其中两个节点被交换,写一个程序恢复这颗BST. 只想到了时间复杂度O(n)空间复杂度O(h) h为树高的解法,还没想到空间O(1)的解法. 交换的情况只有两 ...

  9. 572. Subtree of Another Tree(easy)

    Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no ...

随机推荐

  1. Markdown格式范例

    一个例子: 例子开始 1. 本章学习总结 今天主要学习了三个知识点 封装 继承 多态 2. 书面作业 Q1. java HelloWorld命令中,HelloWorld这个参数是什么含义? 今天学了一 ...

  2. [02] Servlet获取请求和页面跳转

    1.Tomcat和Servlet的关系 之前提到过,Servlet是运行在Web容器里的,Tomcat作为容器的一种,在这里自然也要大概说说两者之间的大致关系. 首先,如上所述,Tomcat是Web应 ...

  3. C# 下搭建最新版OpenCV(Emgu CV)开发环境

    既然是"最新版" 首先当然是去sf找安装包: https://sourceforge.net/projects/emgucv/files/emgucv/ 或着去github主页上c ...

  4. spring mvc 经常出现的错误

    spring mvc 经常出现的错误 spring3.0 和jdk 1.8不是很兼容.有时候会出现一些错误 建议使用spring 4.0和jdk1.8搭配使用 书籍 spring mvc 学习指南上面 ...

  5. 西邮linux兴趣小组2014纳新免试题(三)

    [第三关] 题目 http://sortsth.sinaapp.com/ 分析 查看网页源码,得知题目让找出6种排序算法,每次刷新或提交序列都变化. 15种算法清单: CountingSort     ...

  6. python和C语言混编的几种方式

    Python这些年风头一直很盛,占据了很多领域的位置,Web.大数据.人工智能.运维均有它的身影,甚至图形界面做的也很顺,乃至full-stack这个词语刚出来的时候,似乎就是为了描述它. Pytho ...

  7. 【转】css清除浮动float的三种方法总结,为什么清浮动?浮动会有那些影响?

    摘要: css清除浮动float的三种方法总结,为什么清浮动?浮动会有那些影响?     一.抛一块问题砖(display: block)先看现象: 分析HTML代码结构: <div class ...

  8. EnCase missed some usb activities in the evidence files

    My friend is a developer and her colleague May was suspected of stealing the source code of an impor ...

  9. Linux查找和筛选工具

    本文为原创文章,转载请标明出处 目录 文件名通配符 单字符匹配元字符 ? 多字符匹配元字符 * 字符范围匹配符 [] 排除范围匹配符 [!] 命令中的正则表达式 单字符匹配符 . 单字符或字符串重复匹 ...

  10. 面向对象oop

    类和对象 1.什么是类?什么是对象? 1)现实世界是由很多很多对象组成的 基于对象抽出了类 2)对象:真实存在的单个的个体 类:类型/类别,代表一类个体 3)类中可以包含: 3.1)所有对象所共有的属 ...