isSameTree1

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.

SOLUTION 1 & SOLUTION 2:

以下是递归及非递归解法:

1. 递归解法就是判断当前节点是不是相同,及左右子树是不是相同树

2. 非递归解法使用了先根遍历。这个算法比较简单一点

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
// solution 1:
public boolean isSameTree1(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
} if (p == null || q == null) {
return false;
} return p.val == q.val &&
isSameTree(p.left, q.left) &&
isSameTree(p.right, q.right);
} // Solution 2:
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
} if (p == null || q == null) {
return false;
} Stack<TreeNode> s1 = new Stack<TreeNode>();
Stack<TreeNode> s2 = new Stack<TreeNode>(); s1.push(p);
s2.push(q); while (!s1.isEmpty() && !s2.isEmpty()) {
TreeNode cur1 = s1.pop();
TreeNode cur2 = s2.pop(); // 弹出的节点的值必须相等
if (cur1.val != cur2.val) {
return false;
} // tree1的right节点,tree2的right节点,可以同时不为空,也可以同时为空,否则返回false.
if (cur1.left != null && cur2.left != null) {
s1.push(cur1.left);
s2.push(cur2.left);
} else if (!(cur1.left == null && cur2.left == null)) {
return false;
} // tree1的左节点,tree2的left节点,可以同时不为空,也可以同时为空,否则返回false.
if (cur1.right != null && cur2.right != null) {
s1.push(cur1.right);
s2.push(cur2.right);
} else if (!(cur1.right == null && cur2.right == null)) {
return false;
}
} return true;
}
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/IsSameTree1.java

LeetCode: isSameTree1 解题报告的更多相关文章

  1. LeetCode: Permutations 解题报告

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  2. leetcode—Palindrome 解题报告

    1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...

  3. LeetCode C++ 解题报告

    自己做得LeetCode的题解,使用C++语言. 说明:大多数自己做得,部分参考别人的思路,仅供参考; GitHub地址:https://github.com/amazingyyc/The-Solut ...

  4. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

  5. LeetCode: Subsets 解题报告

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  6. LeetCode: Triangle 解题报告

    Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...

  7. LeetCode: Combinations 解题报告

    Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...

  8. LeetCode: solveSudoku 解题报告

    Sudoku SolverWrite a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are in ...

  9. LeetCode: Anagrams 解题报告

    AnagramsGiven an array of strings, return all groups of strings that are anagrams. Note: All inputs ...

随机推荐

  1. Eclipse 如何导入web项目

      Eclipse 如何导入web项目 CreateTime--2018年3月8日09:07:16 Author:Marydon 方法一:推荐使用 1.将web项目手动拷贝到Eclipse的工作空间下 ...

  2. python之函数用法id(),了解即可

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法id(),了解即可 #http://www.cnblogs.com/hongfei/p ...

  3. 3、eclipse 查看原始类出现The jar file rt.jar has no source attachment解决方法

    因为rt的source在jdk目录的src.zip文件里,将文件设置为jdk下的src.zip就行了.具体如下 Window>Preferences>Java>Installed J ...

  4. [Animations] 快速上手 iOS10 属性动画

    概述 今天要说的UIViewPropertyAnimator, 是iOS10新的API 详细 代码下载:http://www.demodashi.com/demo/10639.html 基础动画, 核 ...

  5. nginx中的502错误

    遇到这种情况,首先看一下慢日志 [17-Aug-2015 13:13:43] WARNING: [pool www] child 27780, script '/data/s.com/index.ph ...

  6. maven 错误处理

    如果是方法找不到或者返回参数变了,那么肯定是包被升级了,那么到仓库下把对应的包删掉,然后maven自动下载最新的. 如果是包找不到,或者类找不到,那么把maven ->update maven可 ...

  7. springmvc编码问题

    web.xml中加入 <filter> <filter-name>encodingFilter</filter-name> <filter-class> ...

  8. JSON与Bean互转

    转自: 关于json与javaBean之间的转换 废话不多说,直接进入主题,json与javaBean之间的互相转换 javaBean转换成json 直接使用提供的方法可以得到json JSONObj ...

  9. HDUOJ----专题训练C

    Problem C Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  10. VC6.0设置注释快捷键

    第一步:工具栏上右键选择Customize(定制),选择“Add-ins and Macro Files(附加项和宏文件)”页,把SAMPLE前面打上钩. 第二步:选择“Commands(命令)”页, ...