From 1faeac7227198d2fca5d157e340890b3163ea53c Mon Sep 17 00:00:00 2001 From: Azureki Date: Wed, 22 Aug 2018 16:46:14 +0800 Subject: [PATCH] 872. Leaf-Similar Trees --- lc872.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 lc872.py diff --git a/lc872.py b/lc872.py new file mode 100644 index 0000000..7c3d4fa --- /dev/null +++ b/lc872.py @@ -0,0 +1,50 @@ + +class TreeNode: + def __init__(self, x): + self.val = x + self.left = None + self.right = None + +class Solution: + def leafSimilar(self, root1, root2): + """ + :type root1: TreeNode + :type root2: TreeNode + :rtype: bool + """ + L1=[] + L2=[] + self.findLeaf(root1,L1) + + self.findLeaf(root2,L2) + # print(L1) + # print(L2) + + return L1 == L2 + + + def findLeaf(self,root,L): + print(root.left,root.right) + if not(root.left or root.right): + print('yes') + L.append(root.val) + print(L) + + if root.left: + self.findLeaf(root.left,L) + if root.right: + self.findLeaf(root.right,L) + + + + +root1=TreeNode(1) +root2=TreeNode(2) + +sol=Solution() + +print(sol.leafSimilar(root1,root2)) + + + + \ No newline at end of file