码迷,mamicode.com
首页 > 其他好文 > 详细

#Leet Code# Same Tree

时间:2014-07-09 20:46:34      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   for   io   

语言:Python

描述:使用递归实现

 

 1 # Definition for a  binary tree node
 2 # class TreeNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7 
 8 class Solution:
 9     # @param p, a tree node
10     # @param q, a tree node
11     # @return a boolean
12     def isSameTree(self, p, q):
13         if (p is None and q is None):
14             return True
15         elif (p is None and q is not None) or (p is not None and q is None):
16             return False
17         else:
18             if (p.val != q.val):
19                 return False
20             else:
21                 return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)

 

 

#Leet Code# Same Tree,布布扣,bubuko.com

#Leet Code# Same Tree

标签:style   blog   color   使用   for   io   

原文地址:http://www.cnblogs.com/mess4u/p/3830125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!