[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. ...
随机推荐
- 3D Slicer 4.7.0 VS 2010 Compile 编译
花了将近一周的时间的,终于在VS2010成功的编译了最新版的3D Slicer 4.7.0,感觉快要崩溃了.Slicer用了20多个外部的库,全都要一起编译,完整编译一次起码要七八个小时,光VS的Ou ...
- 前端基础开发之HTML
简介: 1.HTML是什么? htyper ...
- JavaScript外部函数调用AngularJS的函数、$scope
x 场景: 需要在用FusionCharts画的柱状图中添加点击事件,But弹出框是Angularjs搞的,我想的是直接跳到弹出框的那个路由里,然后在弹出框的控制器中绑定数据即可: /* 点击事件 * ...
- Bootstrap框架 inconfont font-awesome
Bootstrap框架和inconfont.font-awesome使用 iconfont的使用:https://www.cnblogs.com/clschao/articles/10387580.h ...
- RabbitMQ下的生产消费者模式与订阅发布模式
所谓模式,就是在某种场景下,一类问题及其解决方案的总结归纳.生产消费者模式与订阅发布模式是使用消息中间件时常用的两种模式,用于功能解耦和分布式系统间的消息通信,以下面两种场景为例: 数据接入 假设 ...
- 传统DNS的问题与HTTPDNS
本篇学习笔记要掌握两个要点: 传统的 DNS 有很多问题,例如解析慢.更新不及时.因为缓存.转发.NAT 问题导致客户端误会自己所在的位置和运营商,从而影响流量的调度. HTTPDNS 通过客户端 S ...
- 关于初识Java整理
- jquery基础学习之样式篇(一)
一.安装与使用 官网下载,然后引入 <script src="js/jquery-3.3.1.js"></script>,这是生产版本,开发版本替换成min ...
- 洛谷P4495 奇怪的背包 [HAOI2018] 数论
正解:数论+dp 解题报告: 传送门! 首先看到这题,跳无数次,自然而然可以想到之前考过好几次了的一个结论——如果只考虑无限放置i,它可以且仅可以跳到gcd(p,v[i]) 举一反三一下,如果有多个i ...
- LeetCode-52.N-Queen II
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...