100. Same Tree(leetcode)
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)的更多相关文章
- 100. Same Tree(C++)
100. Same Tree Given two binary trees, write a function to check if they are equal or not. Two binar ...
- Device Tree(三):代码分析【转】
转自:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html Device Tree(三):代码分析 作者:linuxer 发布于:201 ...
- 第15个算法-实现 Trie (前缀树)(LeetCode)
解法代码来源 :https://blog.csdn.net/whdAlive/article/details/81084793 算法来源:力扣(LeetCode)链接:https://leetcode ...
- 力扣(LeetCode)删除排序链表中的重复元素II 个人题解
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 思路和上一题类似(参考 力扣(LeetCode)删除排序链表中的重复元素 个人题解)) 只不过这里需要用到一个前 ...
- easyUI之Tree(树)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- 2021.07.19 BZOJ2654 tree(生成树)
2021.07.19 BZOJ2654 tree(生成树) tree - 黑暗爆炸 2654 - Virtual Judge (vjudge.net) 重点: 1.生成树的本质 2.二分 题意: 有一 ...
- 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 ...
- 第五周 Leetcode 99. Recover Binary Search Tree (HARD)
Leetcode99 给定一个 二叉搜索树,其中两个节点被交换,写一个程序恢复这颗BST. 只想到了时间复杂度O(n)空间复杂度O(h) h为树高的解法,还没想到空间O(1)的解法. 交换的情况只有两 ...
- 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 ...
随机推荐
- 201521044091 《Java程序设计》第12周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. Answer: 2. 书面作业 将Student对象(属性:int id, String name,int a ...
- What is uClinux?
What is uClinux? The original uClinux was a derivative of Linux 2.0 kernel intended for microcontrol ...
- sqlserver2012安装过程
第三次安装sqlserver2012 记录下安装过程,受益的人多多指教. 一.以我的系统64位为例,所以先准备安装包,从官网 https://www.microsoft.com/zh-CN/downl ...
- 一个非常好用的框架-AngularJS(一)
前 言 AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.An ...
- JS中基本的一些兼容问题 可能解释的不会太清楚
做兼容注意: 一如果两个都是属性,用逻辑||做兼容 二如果有一个是方法 用三目运算符做兼容 三多个属性或方法封装函数做兼容 一:谷歌浏览器和火狐浏览器鼠标滚动条兼容 1.document.docume ...
- [UIKit学习]00.关于前置知识(storyboard,UIViewController,类扩展,项目属性)
storyboard文件的认识 用来描述软件界面 默认情况下,程序一启动就会加载Main.storyboard 加载storyboard时,会首先创建和显示箭头所指的控制器界面 IBAction和IB ...
- 系统学习java高并发系列三
转载请注明原创出处,谢谢! 首先需要说说线程安全?关于线程安全一直在提,比如StringBuilder和StringBuffer有什么区别? 经常就会出现关于线程安全与线程非安全,可能一直在提自己没有 ...
- 【京东详情页】——原生js爬坑之标签页
一.引言 要做详情页的商品评价等5个li的标签页转换,效果如下: 二.实现原理 有一个特别的地方:上面五个li,但下面只有四个容器(table/div). 设计的目的:无论点哪个li,只有前四个div ...
- String.getBytes(),源码之下,了无秘密
@Deprecated public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) { if (srcBegin ...
- sleep,yield,wait,notify,notifyAll
1.wait,notify,notifyAll是Object的方法.他们必须在同步块中使用,并且当前线程必须已经获取了锁.wait方法,用来释放同步块对象上的锁,并且等待其他的线程唤醒(notify) ...