[LeetCode]题解(python):100-Same Tree
2016-03-07 17:46
513 查看
[b]题目来源:[/b]
https://leetcode.com/problems/same-tree/
[b]题意分析:[/b]
判断两棵树是否相等。
[b]题目思路:[/b]
用递归的思想,先判断根节点,再判断左右子树。
[b]代码(python):[/b]
View Code
https://leetcode.com/problems/same-tree/
[b]题意分析:[/b]
判断两棵树是否相等。
[b]题目思路:[/b]
用递归的思想,先判断根节点,再判断左右子树。
[b]代码(python):[/b]
# 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 """ if p == None: return q == None if q == None: return p == None if p.val != q.val: return False else: return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
View Code
相关文章推荐
- Python Basics
- python2x的str/unicode转换以及python3x中的str/bytes转换
- python Queue学习
- [LeetCode]题解(python):099-Recover Binary Search Tree
- github/python/ show me the code 25题(一)
- Python任务调度模块 – APScheduler
- python解析json数据
- 关于Python的super用法研究
- VPython—旋转坐标系
- [LeetCode]题解(python):098- Validate Binary Search Tree
- python入门(二)
- Python一日一练05----怒刷点击量
- 个人测试用的python工具类
- python可视化
- python 爬虫好文 urllib cookie beautifulsoap
- Python 使用简单语法
- Python使用SQLite
- python成长之路第三篇(3)_内置函数及生成器迭代器
- python unittest错误接口直接报错的方法
- python编写网页爬虫脚本并实现APScheduler调度