[LeetCode] 100. Same Tree_Easy tag: DFS
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 思路基本的DFS, 都空, 为True, 都不为空, 判断value一样, 并且recursive children, 否则False 1. Constraints
1) 可以都为None => True 2. Ideas DFS O(n) 3. Code
class Solution:
def sameTree(self, p, q):
if not p and not q:
return True
if p and q and p.val == q.val:
return self.sameTree(p.left, q.left) and self.sameTree(p.right, q.right)
return False
[LeetCode] 100. Same Tree_Easy tag: DFS的更多相关文章
- [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- [LeetCode] 559. Maximum Depth of N-ary Tree_Easy tag: DFS
Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...
- [LeetCode] 110. Balanced Binary Tree_Easy tag: DFS
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- [LeetCode] 111. Minimum Depth of Binary Tree_Easy tag:DFS
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] 104. Maximum Depth of Binary Tree_Easy tag: DFS
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LeetCode] 872. Leaf-Similar Trees_Easy tag: DFS
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form ...
- [LeetCode] 721. Accounts Merge_Medium tag: DFS recursive
Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- [LeetCode] 112. Path Sum_Easy tag: DFS
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] 364. Nested List Weight Sum II_Medium tag:DFS
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
随机推荐
- J - 哈密顿绕行世界问题
一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市. Input 前20行的第i行有3个数,表示与第i个城市相邻的3个城市.第20行 ...
- E - 487--3279
Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is ...
- 3、CSS属性组成和作用
3:CSS属性组成和作用 学习目标 1.css属性和属性值的定义 2.css文本属性 3.css列表属性 4.css背景属性 5.css边框属性 6.css浮动属性 一.css属性和属性值的定义 属性 ...
- Linux环境变量与文件查找
作业: 找出/etc目录下所有以.list结尾的文件 代码:locate /etc/\*.list sudo find /etc/ -name \*.list
- eclipse中一个项目引用另一个项目的方法(申明:来源于网络)
eclipse中一个项目引用另一个项目的方法(申明:来源于网络) 地址:http://blog.csdn.net/a942980741/article/details/39990699
- ROS中.launch文件的remap标签详解
https://www.cnblogs.com/LiuQiang921202/p/7679943.html
- A little issue in Mathematical Thought from Ancient to Modern Times, Vol. 3
At P985 of the book, says But there are cuts that are not determined by rational numbers. If we put ...
- Hive日志(Hive Logging)--hive GettingStarted翻译
Hive uses log4j for logging. By default logs are not emitted to the console by the CLI. The default ...
- [archlinux] 迁移T7从T460s到T470
这已经不是第一次做OS的迁移了,T7早已经迁移过多台设备了.所以,其实只需要如下三步: 1. rsync 我一直有全系统备份的习惯,T7一直会不定期的全系统rsync到Tstation上面去.所以我 ...
- [development][C] linux 设置线程名称
两个API, 都是linux的. 不是POSIX, 是GNU? 傻傻搞不清楚. 1. pthread_setname_np / pthread_setname_np 2. ptctl 带 PR_GE ...