这道题是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. WebForm随笔

    一般处理程序中获取页面所传的值:int id = Convert.ToInt32(context.Request["id"]); 后台获取页面所传的值:int id = Conve ...

  2. Bootstrap基础知识学习

    Bootstrap中文网 http://www.bootcss.com/ Bootstrap菜鸟教程 http://www.runoob.com/bootstrap/bootstrap-tutoria ...

  3. 一起来学Spring Cloud | 第三章:服务消费者 (负载均衡Ribbon)

    一.负载均衡的简介: 负载均衡是高可用架构的一个关键组件,主要用来提高性能和可用性,通过负载均衡将流量分发到多个服务器,多服务器能够消除单个服务器的故障,减轻单个服务器的访问压力. 1.服务端负载均衡 ...

  4. js作用域及对象以及一些常用技巧

    回顾 流程控制(语句) 分支 if () {    }​if () {    } else {    }​if () {    } else if () {    } else if () {     ...

  5. CF1066B Heaters

    思路: 从左向右贪心选择能覆盖当前位置的最靠右的那个heater即可,和poj radar installation类似. 实现: #include <iostream> #include ...

  6. texlive安装

    本人电脑系统win8.1,安装texlive2016的时候报错"Can't spawn "cmd.exe": No such file or directory at.. ...

  7. C 函数库 (libc,glibc,uClibc,newlib)

    glibc glibc和libc都是Linux下的C函数库,libc是Linux下的ANSI C的函数库:glibc是Linux下的GUN C的函数库:GNU C是一种ANSI C的扩展实现.ANSI ...

  8. Android学习总结(六)———— 发送自定义广播

    一.两种广播类型 2.1 标准广播 是一种完全异步执行的广播,在广播发出去之后,所有的广播接收器几乎都会在同一时刻接收到这条广播消息,因此它们之间没有任何先后顺序可言.这种广播的效率会比较高,但同时也 ...

  9. UVA 1347 Tour 双调TSP

    TSP是NP难,但是把问题简化,到最右点之前的巡游路线只能严格向右,到最右边的点以后,返回的时候严格向左,这个问题就可以在多项式时间内求出来了. 定义状态d[i][j]表示一个人在i号点,令一个人在j ...

  10. iterator与iterable

    用Iterator模式实现遍历集合Iterator模式是用于遍历集合类的标准访问方法.它可以把访问逻辑从不同类型的集合类中抽象出来,从而避免向客户端暴露集合的内部结构.例如,如果没有使用Iterato ...