[抄题]:

Given two binary trees, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

Example 1:

Input:     1         1
/ \ / \
2 3 2 3 [1,2,3], [1,2,3] Output: true

Example 2:

Input:     1         1
/ \
2 2 [1,2], [1,null,2] Output: false

Example 3:

Input:     1         1
/ \ / \
2 1 1 2 [1,2,1], [1,1,2] Output: false

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

基础弱到没有recursion的概念

[一句话思路]:

recursion就是嵌套

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 从正反两方面想,把所有情况都想到:p,q val相不相等,p,q不空、一个空、2个空

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

要有默认情况

[复杂度]:Time complexity: O(n) Space complexity: O(n)

所有的点走一遍,时间复杂度就是n

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

return ((p.val == q.val) && (isSameTree(p.left, q.left)) && (isSameTree(p.right, q.right)));  

[其他解法]:

非递归,用stack,很麻烦 属于没事找事

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
//both are null
if (p == null && q == null) return true;
//just one null
if (p == null || q == null) return false;
//p.val == q.val, recursion
if (p.val == q.val) return (isSameTree(p.left, q.left)) && (isSameTree(p.right, q.right));
//p.val != q.val
return false;//default
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if (p == null && q != null) return false;
if (p != null && q == null) return false; return ((p.val == q.val) && (isSameTree(p.left, q.left)) && (isSameTree(p.right, q.right)));
}
}

100. Same Tree同样的树的更多相关文章

  1. LeetCode 100. Same Tree (判断树是否完全相同)

    100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...

  2. 100 Same Tree 相同的树

    给定两个二叉树,写一个函数来检查它们是否相同.如果两棵树在结构上相同并且节点具有相同的值,则认为它们是相同的.示例 1:输入 :      1         1             / \    ...

  3. [leetcode]100. Same Tree相同的树

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

  4. LeetCode 100. Same Tree相同的树 (C++)

    题目: Given two binary trees, write a function to check if they are the same or not. Two binary trees ...

  5. <LeetCode OJ> 100. Same Tree

    100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...

  6. 100. Same Tree(C++)

    100. Same Tree Given two binary trees, write a function to check if they are equal or not. Two binar ...

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

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

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

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

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

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

随机推荐

  1. Oracle 之 配置HugePages内存

    HugePages是通过使用大页内存来取代传统的4kb内存页面,使得管理虚拟地址数变少,加快了从虚拟地址到物理地址的映射以及通过摒弃内存页面的换入换出以提高内存的整体性能.尤其是对于8GB以上的内存以 ...

  2. CentOS 添加常用 yum 源(转)

    CentOS 的官方源去掉了一些与版权有关的软件,因此想要安装这些软件或者手动下载安装,或者使用其他源. 下面我推荐常用的两个源, 这两个源基本可以满足一般服务器的使用需求. 首先, 添加源之前要确定 ...

  3. win7 + python2.7 安装scipy

    问题: 直接pip install scipy将不能正确安装,缺少文件 方法: 下载  "scipy‑0.19.0‑cp27‑cp27m‑win_amd64.whl"[90多M] ...

  4. mac下的virtualbox启动失败处理

    不知从哪个版本开始,mac下的virtualbox建立vm以后,启动就提示什么驱动没有加载,google后,解决如下 sudo /Library/Application\ Support/Virtua ...

  5. ipvsadm的命令参考

    相信很多同学和我差不多,半桶水,貌似在配置lvs双机的时候,直接用的keepalived,ipvsadm就用来看看,感觉没啥用,今天无聊到处逛发现,某大神说,keepalived只是ipvsadm的一 ...

  6. JS 实现关闭浏览器

        $('#exitSystem').on('click',function(){ if(confirm("确定要退出系统并关闭浏览器吗?")){ //关闭浏览器的方法只适用i ...

  7. [Java][Web]Web 工程中的各类地址的写法

    // 1. request.getRequestDispatcher("/index.html").forward(request,response); // 以 / 开头,对于浏 ...

  8. Vue踩坑记录册

    1.vue-cli+webpack项目 修改项目名称 解决办法: 1 删除 node_modules 文件夹(如果修改项目名称,需要在在package.json中修改对应的name) 2 重新安装依赖 ...

  9. 第四章 istio快速入门(快速安装)

    4.1 环境介绍 K8s 1.9 以上版本. 4.2 快速部署Istio 下载:  https://github.com/istio/istio/releases/,  下载 1.1.0-snapsh ...

  10. FBVector

    folly/FBVector.h Simply replacing std::vector with folly::fbvector (after having included the folly/ ...