LeetCode: isSameTree1 解题报告
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 解题报告的更多相关文章
- LeetCode: Permutations 解题报告
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- leetcode—Palindrome 解题报告
1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...
- LeetCode C++ 解题报告
自己做得LeetCode的题解,使用C++语言. 说明:大多数自己做得,部分参考别人的思路,仅供参考; GitHub地址:https://github.com/amazingyyc/The-Solut ...
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
- LeetCode: Subsets 解题报告
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- LeetCode: Triangle 解题报告
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- LeetCode: Combinations 解题报告
Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...
- LeetCode: solveSudoku 解题报告
Sudoku SolverWrite a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are in ...
- LeetCode: Anagrams 解题报告
AnagramsGiven an array of strings, return all groups of strings that are anagrams. Note: All inputs ...
随机推荐
- Java使用JDBC连接随意类型数据库(mysql oracle。。)
package cn.liz.test; import java.io.InputStream; import java.sql.Connection; import java.sql.Driver; ...
- 各大主流.Net的IOC框架
Autofac下载地址:http://code.google.com/p/autofac/ Castle Windsor下载地址:http://sourceforge.net/projects/cas ...
- linux利器expect的使用
1.什么是expect在做系统管理时,我们很多时候需要输入密码,例如:连接 ssh,连接ftp,那么如何能做到不输入密码,我们需要有一个工具,能代替我们实现与终端的交互,它能够代替我们实现与终端的交互 ...
- 深入PHP内核之数组
定义: PHP 中的数组实际上是一个有序映射.映射是一种把 values 关联到 keys 的类型.此类型在很多方面做了优化,因此可以把它当成真正的数组,或列表(向量),散列表(是映射的一种实现),字 ...
- java多线程(一)之继承Thread类
一.概述 进程:正在执行的应用程序 线程:进程的执行单元,执行路径 单线程:一个应用程序只有一条执行路径 多线程:一个应用程序有多条执行路径 二.两种实现方式, 下面为第一种方式: 继承Thread类 ...
- HDUOJ--1865 1string
1sting Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- java容器详细解析(转)
:在java开发中我们肯定会大量的使用集合,在这里我将总结常见的集合类,每个集合类的优点和缺点,以便我们能更好的使用集合.下面我用一幅图来表示 其中淡绿色的表示接口,红色的表示我们经常使用的类. 1: ...
- php 文件上传,下载
文件下载: html: <html> <body> <a href="1.rar">下载1.rar</a> <br /> ...
- LevelDB场景分析4--BackgroundCompaction
1.DBImpl::Open uint64_t new_log_number = impl->versions_->NewFileNumber(); WritableF ...
- ubuntu16.4搭建tensorflow环境
1 说明: 本机配置:显卡gtx970,ubuntu16.4.1+cuda8.0+cudnn v5+tensorflow0.11 1. 下载 1.1 系统镜像 由于我尝试了ubuntu14.04,安装 ...