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 ...
随机推荐
- POJ2564:Edit Step Ladders
浅谈\(Trie\):https://www.cnblogs.com/AKMer/p/10444829.html 题目传送门:http://poj.org/problem?id=2564 记\(f[i ...
- SharePoint2013集成Exchange之任务同步
SharePoint可以将任务列表到outlook中,但在sharepoint 2013 上这个功能似乎不是很好用,如下图所示,点击任务列表的"同步到Outlook"按钮: 在弹出 ...
- 【备忘录】Sublime Text编辑器如何在选中的多行行首增加字符串
如题:上面的代码,想在每一行的开头加上一个字符 * 如下: 操作步骤如下: 1.选中要操作的行(我这里Ctrl+A) 2.Ctrl+Shift+L (待操作状态) 3.方向键← (操作这步骤后,可 ...
- iso网络模型
tcp/ip知识 1.iOS七层模型 应用层 表示层 应用层 ssh httpssl tls ftp mime html snmp 会话层 传输层 传输层 tcp udp 网络层 网络层 ipv6 i ...
- python TypeError: unsupported operand type(s) for +: 'int' and 'str' [closed]
TypeError: unsupported operand type(s) for +: 'int' and 'str' [closed] sql="insert into auto_tr ...
- Ladder面积
package com.hanqi; import javax.swing.plaf.synth.SynthSeparatorUI; //梯形 public class Ladder { double ...
- struts全包导入问题
web.xml如下: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&q ...
- Mac OS Virtualbox 倒入 ova 镜像文件
原文:https://www.zhihu.com/question/40785995 ------------------ 自己亲测 --------------------- 把 windows 系 ...
- Hive语句执行优化-简化UDF执行过程
Hive会将执行的SQL语句翻译成对应MapReduce任务,当SQL语句比较简单时,性能还是可能处于可接受的范围.但是如果涉及到非常复杂的业务逻辑,特别是通过程序的方式(一些模版语言生成)生成大 ...
- 几个常用的Linux监控脚本
几个常用的Linux监控脚本 几个常用的Linux监控脚本下面是几个主机监控的脚本,大家可以根据自己的情况再进行修改,希望能给大家一点帮助.1.查看主机网卡流量#!/bin/bash#network# ...