首页 > 编程语言 > 详细

[leetcode]Same Tree @ Python

时间:2014-05-26 10:29:48      阅读:496      评论:0      收藏:0      [点我收藏+]

原题地址:https://oj.leetcode.com/problems/same-tree/

题意:判断两棵树是否是同一棵树。

解题思路:这题比较简单。用递归来做。首先判断两个根节点的值是否相同,如果相同,递归判断根的左右子树。

代码:

bubuko.com,布布扣
# Definition for a  binary tree node
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    # @param p, a tree node
    # @param q, a tree node
    # @return a boolean
    def isSameTree(self, p, q):
        if p == None and q == None: return True
        if p and q and p.val == q.val:
            return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
        return False
bubuko.com,布布扣

 

[leetcode]Same Tree @ Python,布布扣,bubuko.com

[leetcode]Same Tree @ Python

原文:http://www.cnblogs.com/zuoyuan/p/3747148.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!