[LeetCode]题解(python):100 Same Tree
题目来源
https://leetcode.com/problems/same-tree/
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
题意分析
Input: twon binary tree
Output: equal?
Conditions:判断两个二叉树是不是相等。
题目思路
逐一遍历,看是否相等。
AC代码(Python)
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
def dfs(p, q):
if p == None and q != None:
return False
if p != None and q == None:
return False
if p == None and q == None:
return True
if p.val != q.val:
return False
return dfs(p.left, q.left) and dfs(p.right, q.right) return dfs(p,q)
[LeetCode]题解(python):100 Same Tree的更多相关文章
- <LeetCode OJ> 100. Same Tree
100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...
- LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number
100. Same Tree /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; ...
- [LeetCode 题解]: Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)
题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...
- LeetCode题解之Balanced Binary Tree
1.题目描述 2.问题分析 DFS. 3.代码 bool isBalanced(TreeNode* root) { if (root == NULL) return true; && ...
- LeetCode题解:Flatten Binary Tree to Linked List:别人的递归!
总是在看完别人的代码之后,才发现自己的差距! 我的递归: 先把左侧扁平化,再把右侧扁平化. 然后找到左侧最后一个节点,把右侧移动过去. 然后把左侧整体移到右侧,左侧置为空. 很复杂吧! 如果节点很长的 ...
- LeetCode题解之Univalued Binary Tree
1.题目描述 2.问题分析 遍历一遍树,然后将所有节点的数值放入到一个set中,最后检查set中元素的个数是否为1. 3.代码 bool isUnivalTree(TreeNode* root) { ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- [LeetCode 题解]: Binary Tree Preorder Traversal
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a bi ...
- [LeetCode 题解]: Symmetric Tree
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a ...
随机推荐
- python 代码片段
print 'dongshen' for word in ['capitalize','these','words']: print word.upper() for i in range(0,5): ...
- BZOJ3463 : [COCI2012] Inspector
考虑将序列分成$\sqrt{n\log n}$块,每块维护下凸壳,修改时在相应块打上需要修改的标记. 查询时,对于两端零散部分暴力查询. 对于中间的块,如果有修改标记,则暴力重构. 然后在凸壳上查询时 ...
- js html5推送 实例
<!DOCTYPE html> <html> <head> <title>Simple Webkit notification exampl ...
- wp控件
导航控件 Silverlight的Windows Phone应用程序是基于一种可以让用户在不同页面内容间来回导航的页面模型.这个模型是基于其中的frame控件,而页面间的导航就是靠它. 下面的表格列出 ...
- 【BZOJ】1269: [AHOI2006]文本编辑器editor(Splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1269 这题RE2次啊,好不爽啊,我一直以为是splay的问题,其实是数组开小了......(我老犯这 ...
- hiho#1145 : 幻想乡的日常
描述 幻想乡一共有n处居所,编号从1到n.这些居所被n-1条边连起来,形成了一个树形的结构. 每处居所都居住着一个小精灵.每天小精灵们都会选出一个区间[l,r],居所编号在这个区间内的小精灵一起来完成 ...
- 使用javabean连接数据库时遇到的问题
1.whitespace问题.是便签了每空格如: 就会出现下面的问题.contentType前每空格.空格后就解决了 2. 这是部署问题,关闭,重新部署 追问 怎么部署? 回答 1.选中项目 F5(e ...
- 在Copy-Item中集成认证信息以拷贝文件
$source = "c:\XXX.XXX" $pw = ConvertTo-SecureString '密码' -AsPlainText -Force $Creds = New- ...
- 更新AD对象属性值
1. 对于Set-ADUser不包含的对象属性,可以采用replace来操作 Set-ADUser -Identity 'UserA' -Replace @{userWorkstations = 'C ...
- Javascript 笔记与总结(2-13)定时器 setTimeout 和 setInterval
定时器可以让 js 效果每隔几秒钟执行一次或者 n 秒之后执行某一个效果.定时器不属于 javascript,是 window 对象提供的功能. setTimeout 用法: window.setTi ...