100. Same Tree同样的树
[抄题]:
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就是嵌套
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 从正反两方面想,把所有情况都想到: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同样的树的更多相关文章
- 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 ...
- 100 Same Tree 相同的树
给定两个二叉树,写一个函数来检查它们是否相同.如果两棵树在结构上相同并且节点具有相同的值,则认为它们是相同的.示例 1:输入 : 1 1 / \ ...
- [leetcode]100. Same Tree相同的树
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- LeetCode 100. Same Tree相同的树 (C++)
题目: Given two binary trees, write a function to check if they are the same or not. Two binary trees ...
- <LeetCode OJ> 100. Same Tree
100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...
- 100. Same Tree(C++)
100. Same Tree Given two binary trees, write a function to check if they are equal or not. Two binar ...
- paip.tree 生成目录树到txt后的折叠查看
paip.tree 生成目录树到txt后的折叠查看 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.ne ...
- 【BZOJ2588】Count On a Tree(主席树)
[BZOJ2588]Count On a Tree(主席树) 题面 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第 ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
随机推荐
- 安装Zookeeper(集群版)
一.环境介绍(3台虚拟机) IP Hostname 192.168.2.14 javaweb04 192.168.2.15 javaweb05 192.168.2.16 javaweb06 二.配置文 ...
- Windows下生成自签名证书
最近通过openssl生成了自签名的证书,总结成下面这张图. 说明:下载openssl0.9.8之后解压,然后运行bin\openssl.exe进入openssl运行环境,然后按上图中顺序执行命令.( ...
- RK3288 通过指令查看当前显示内容(framebuffer)
$ adb shell root@xxx:/ # cd /dev/graphics cd /dev/graphics root@xxx:/dev/graphics # ls ls fb0 fb1 fb ...
- 基于Oracle的EntityFramework的WEBAPI2的实现(一)——准备工作
目前在.net的范围内,好的而且方便的ORM的真的不是很多,与VS集成方便的也就当属EntityFramework(以下简称EF,不知道为什么,总EF这个缩写好不专业).但是,好多公司使用的又是ORA ...
- Python控制台输出不换行(进度条等)
sys.stdout.write('\r'+str) sys.stdout.flush() time.sleep(1)
- 让html标签显示在页面上
用 <xmp></xmp> 标签包起来,里面的所有文字会原样显示出来 <xmp><b>1</b><div>2</div&g ...
- python多分类预测模版,输出支持度,多种分类器,str的csv转float
预测结果为1到11中的1个 首先加载数据,训练数据,训练标签,预测数据,预测标签: if __name__=="__main__": importTrainContentdata( ...
- c# 判断文件是否已使用
string path = Server.MapPath(" PDFs"); bool tfOpenTemp= IsFileInUse(path + " /Doc1.pd ...
- http 和 https 区别?
1. HTTP 的URL 以http:// 开头,而HTTPS 的URL 以https:// 开头2. HTTP 是不安全的,而 HTTPS 是安全的3. HTTP 标准端口是80 ,而 HTTPS ...
- 文档主题生成模型(LDA)
一.问题描述 1.1文本建模相关 统计文本建模的目的其实很简单:就是估算一组参数,这组参数使得整个语料库出现的概率最大.这是很简单的极大似然的思想了,就是认为观测到的样本的概率是最大的.建模的目标也是 ...