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 ...
随机推荐
- Javascript获取当月的天数
var d = new Date(); var curMonthDays = new Date(d.getFullYear(), (d.getMonth() + 1), 0).getDate(); a ...
- bash: fork: Resource temporarily unavailable
Last login: Wed Jul 26 09:19:11 2017 from ... -bash: fork: Resource temporarily unavailable -bash-3. ...
- 题目:vbs批量开通工具,实现vbs开通的ux设计和流程调度
题目:vbs批量开通工具,实现vbs开通的ux设计和流程调度 需求点:支持开通前检查(检查失败不允许开站),开通过程监控,开通后业务检验,失败后重新开通,支持部分站点开通(比如用户导入的模板中有10个 ...
- PHP生成缩略图、加水印
<?php class ThumbWaterImages{ /** * 生成缩略图/加水印 * classname ThumbWaterImages * datetime:2015-1-15 * ...
- checkbox选择框如果被选中value值就可以传过去,没有被选中value就不能穿过去(调试了近一天,坑爹的说)
因为要适合各种分辨率,所以将原来的单选按钮radio换成单个的checkbox
- GoogleMapsV3-----基础地图(自定义消息提示OverlayView) (转)
<html> <head> <title> </title> <title></title> <style type ...
- Linux命令-服务管理命令:chkconfig
chkconfig --list 查看服务自启动状态列表,等同于查看服务列表 设置某一个服务为自启动服务: chkconfig 服务名 on 修改服务的启动级别为3,,5 查看某一个服务时候已经运行了 ...
- 分析一帧基于UDP的TFTP协议帧
下图是UDP的段格式: 相比TCP段格式,UDP要简单得多,也没啥好说的,需要注意的是UDP数据长度指payload加上首部的长度. 下面分析一帧基于UDP的TFTP协议帧: 以太网首部 0000: ...
- Python max() 函数
描述 max() 函数返回给定参数的最大值,参数可以为序列. 语法 以下是 max() 函数的语法: max( x, y, z, .... ) 参数 x -- 数值表达式. y -- 数值表达式. z ...
- Git 基本分支规范
基本代码分支应该分为两类,一类是主要分支,包括线上主分支 Master 和开发主分支Develop:另一类是辅助分支,包括测试分支 Release,线上紧急修复分支 Hotfix,以及功能开发分支 F ...