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. 201521123106 《Java程序设计》第13周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu ...

  2. What is uClinux?

    What is uClinux? The original uClinux was a derivative of Linux 2.0 kernel intended for microcontrol ...

  3. java实现oracle数据库基本操作

    import java.sql.*; import java.util.ArrayList; import java.util.List; //使用jdbc连接 public class TestOr ...

  4. JDBC数据库之添加数据

    通过JDBC向数据库中添加数据,可以使用INSERT语句实现插入数据SQL语句,对于SQL语句中的参数可以只用占位符"?"代替,然后通过PreparedStatement对其赋值以 ...

  5. Azure ARM (16) 基于角色的访问控制 (Role Based Access Control, RBAC) - 使用默认的Role

    <Windows Azure Platform 系列文章目录> 今天上午刚刚和客户沟通过,趁热打铁写一篇Blog. 熟悉Microsoft Azure平台的读者都知道,在老的Classic ...

  6. MyBatis学习(一)简介及入门案例

    1.什么是MyBatis? MyBatis是一个支持普通SQL查询,存储过程,和高级映射的优秀持久层框架.MyBatis去掉了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBati ...

  7. java 面向对象 2

    一.JAVA类的定义 JAVA里面有class关键字定义一个类,后面加上自定义的类名即可.如这里定义的person类,使用class person定义了一个person类,然后在person这个类的类 ...

  8. 记一次使用快速幂与Miller-Rabin的大素数生成算法

    大家都知道RSA的加密的安全性就是能够找到一个合适的大素数,而现在判断大素数的办法有许多,比如Fermat素性测试或者Miller-Rabin素性测试,而这里我用了Miller-Rabin素性测试的算 ...

  9. Python网络编程socket练习(TCP)

    服务器端:server.py # -*- coding: utf-8 -*- from socket import * HOST='' PORT=5000 BUFF_SIZE=1024 ADDR=(H ...

  10. 应试记录2(没有转载标注,NOIP2016复赛过后自动删除)

    #include<stdio.h> #include<string.h> int main() { ]; memset(a, , sizeof(a)); ;i<=;i++ ...