这道题是LeetCode里的第100道题。

这是题目:

给定两个二叉树,编写一个函数来检验它们是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

示例 1:

输入:       1         1
/ \ / \
2 3 2 3 [1,2,3], [1,2,3] 输出: true

示例 2:

输入:      1          1
/ \
2 2 [1,2], [1,null,2] 输出: false

示例 3:

输入:       1         1
/ \ / \
2 1 1 2 [1,2,1], [1,1,2] 输出: false

同样是个简单题,按照惯例同时给出递归和迭代的解法,个人感觉迭代会比递归快一点。

这是代码:

递归法:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(p==NULL&&q==NULL)return true;
else if(p==NULL||q==NULL)return false; return (p->val==q->val)
&&isSameTree(p->left,q->left)
&&isSameTree(p->right,q->right);
}
};

迭代法:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(p==NULL&&q==NULL)return true;
else if(p==NULL||q==NULL)return false; stack<TreeNode*>stp;
stack<TreeNode*>stq;
stp.push(p);
stq.push(q);
TreeNode* tnp;
TreeNode* tnq;
while(stp.size()!=0&&stq.size()!=0){
tnp=stp.top();tnq=stq.top();
stp.pop();stq.pop();
if(tnp->val!=tnq->val)return false; if(tnp->left!=NULL||tnq->left!=NULL){
if(tnp->left==NULL||tnq->left==NULL)return false;
stp.push(tnp->left);stq.push(tnq->left);
}
if(tnq->right!=NULL||tnp->right!=NULL){
if(tnq->right==NULL||tnp->right==NULL)return false;
stp.push(tnp->right);stq.push(tnq->right);
}
}
return true;
}
};

这是结果:

递归法:

迭代法:

感觉这结果有水分,可能是案列过少的原因。

这是总结:

起初没有想到值的问题,造成一次提交失误,然后我就是想说用递归真爽,迭代法的话也可以用数组或者是队列,反正基本思想也就是我写的那样。

【LeetCode】Same Tree(相同的树)的更多相关文章

  1. [LeetCode] Same Tree 判断相同树

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  2. [LeetCode] Symmetric Tree 判断对称树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  3. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  4. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  5. paip.tree 生成目录树到txt后的折叠查看

    paip.tree 生成目录树到txt后的折叠查看 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.ne ...

  6. 【BZOJ2588】Count On a Tree(主席树)

    [BZOJ2588]Count On a Tree(主席树) 题面 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第 ...

  7. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  8. BZOJ_2212_[Poi2011]Tree Rotations_线段树合并

    BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...

  9. BZOJ_1803_Spoj1487 Query on a tree III_主席树+dfs序

    BZOJ_1803_Spoj1487 Query on a tree III_主席树 Description You are given a node-labeled rooted tree with ...

  10. 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树

    平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...

随机推荐

  1. 移动端REM布局模板(阿里高清方案)

    移动端REM布局模板(阿里高清方案),蛮好的,转自: http://www.jianshu.com/p/985d26b40199 . <!DOCTYPE html> <html la ...

  2. js js弹出框、对话框、提示框、弹窗总结

    js弹出框.对话框.提示框.弹窗总结 一.JS的三种最常见的对话框 //====================== JS最常用三种弹出对话框 ======================== //弹 ...

  3. 一步步实现自己的ORM(四)

    通过前3章文章,大致对ORM有一定的了解,但也存在效率低下(大量用了反射)和重复代码,今天我们要对ORM进行优化. 具体流程如下: 我们优化的第一个就是减少反射调用,我的思路是定义一个Mapping, ...

  4. CSS3基础知识学习

    CSS3动画例子展示 http://www.17sucai.com/pins/demoshow/13948 HTML5和CSS3特效展示 http://www.html5tricks.com/30-m ...

  5. CF1072B Curiosity Has No Limits

    思路: 对于序列t,只要第一个数确定了,后续的数也随之确定了.枚举四种情况即可.实现: #include <iostream> #include <vector> using ...

  6. BZOJ1485: [HNOI2009]有趣的数列(Catalan数,质因数分解求组合数)

    题意 挺简洁的. 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足a1<a3<…<a ...

  7. LR中订单流程脚本

    Action(){ /* 主流程:登录->下订单->支付订单->获取订单列表 定义事物 1)登录 2)下订单 3)支付订单 4)获取订单列表 接口为:application/json ...

  8. java代码(生成日历时间)

    package test; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; p ...

  9. 洛谷 P2916 [USACO08NOV]为母牛欢呼Cheering up the Cows

    题目描述 Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths tha ...

  10. Shell脚本之for循环、while循环,if语句、case语句

    1. for循环一般格式: 格式1: for((条件)) do 动作 done 格式2: for 变量名 in 范围 do 动作 done1234567891011121314实验:##1. 输出数字 ...