标签:改进 pre app OLE val code extend bsp []
class Solution(object): def isSameTree(self, p, q): """ :type p: TreeNode :type q: TreeNode :rtype: bool """ if self.PreOrder(p)==self.PreOrder(q): return True return False def PreOrder(self,cur): if not cur: return [] res=[] res.extend(self.PreOrder(cur.left)) res.append(cur.val) res.extend(self.PreOrder(cur.right)) return res
有错,先序相同,树不一定同
public boolean isSameTree2(TreeNode p, TreeNode q) { return (p != null && q != null && p.val == q.val && isSameTree2(p.left, q.left) && isSameTree2(p.right, q.right)) || (p == null && q == null); }
1.先要有正确的求解思路
2.递归是常用的套路
标签:改进 pre app OLE val code extend bsp []
原文地址:https://www.cnblogs.com/captain-dl/p/10222961.html