sys.path.append(os.path.abspath(os.path.dirname(os.getcwd()))) sys.path.append(os.path.abspath(os.path.dirname(os.path.dirname(os.getcwd())))) from tree_category.binary_tree import BinaryTree class JudgeTwoTrees(object): def __init__(self, tree, tree1): self.tree = tree self.tree1 = tree1 def judge(self, root, root1): if root == None and root1 == None: return True if root == None or root1 == None: return False return (root.data == root.data and self.judge(root.left, root1.left) and self.judge(root.right, root1.right)) if __name__ == "__main__": t = BinaryTree() for i in range(10): t.add(i) t1 = BinaryTree() for i in range(10): t1.add(i) m = JudgeTwoTrees(t, t1) print(m.judge(t.root, t1.root))
self.tree = tree self.input = input def get_one_path(self, root): # Write your code here path = '' res = [] self.paths(root, path, res) return res def paths(self, root, path, res): if root is None: return self.input -= root.data path += str(root.data) if root.left is not None: self.paths(root.left, path + '->', res) if root.right is not None: self.paths(root.right, path + '->', res) if root.left is None and root.right is None: if self.input == 0: res.append(path) if __name__ == "__main__": t = BinaryTree() for i in range(10): t.add(i) m = GetOnePath(t, 11) print(m.get_one_path(t.root))
import os import sys sys.path.append(os.path.abspath(os.path.dirname(os.getcwd()))) from tree_category.binary_tree import BinaryTree class JudgeMirror(): def __init__(self,tree,tree1): self.tree = tree self.tree1 = tree1 def judge(self,root,root1): if root == None and root1 == None: return True if root == None or root1 == None: return False if root.data != root1.data: return False else: return self.judge(root.left,root.right) and self.judge(root.right,root.left) if __name__ == "__main__": t = BinaryTree() for i in range(10): t.add(i) t2 = BinaryTree() for i in range(10): t2.add(i) m = JudgeMirror(t,t2) print(m.judge(t.root,t2.root))