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. win7 登录后只能使用“临时配置文件”,原来的配置文件无法启用!

    这个问题,修改注册表就可以解决. 开始-> 运行-> 输入regedit,回车, 在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\Cur ...

  2. 网路总结01-HTTP协议和NSURLConnection

  3. python之模块csv之 读取CSV文件(reader和DictReader2个方法)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #读取CSV文件(reader和DictReader2个方法) import csv #csv文件,是一种常用 ...

  4. java Socket Tcp示例三则(服务端处理数据、上传文件)

    示例一: package cn.itcast.net.p5.tcptest; import java.io.BufferedReader;import java.io.IOException;impo ...

  5. HDUOJ---hello Kiki

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. POJ----The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 18890   Accepted: 9150 Des ...

  7. 基于NOPI的Execl模板转换类,直接将Execl模板转换对应的Entity

    1.创建实体属性标记 public class CellAttribute : Attribute { /// <summary> /// /// </summary> /// ...

  8. OAF_OAF Framework常用函数汇总(概念)

    2014-12-31 Created By BaoXinjian

  9. Android studio 使用NDK工具实现JNI编程

    前言: Android开发中常常会使用到第三方的.so库.在使用.so库的时候就要用到JNI编程.JNI是Java Native Interface的缩写.它提供了若干的API实现了Java和其它语言 ...

  10. 2-String to Integer (atoi)

    实现atoi这个函数, public int atoi(String str),传入字符串str可以返回整数,请仔细考虑一下字符串的各种情况! String to Integer: Case分析 正常 ...