def recoverTree(self, root): """ :type root: TreeNode :rtype: void Do not return anything, modify root in-place instead. """ tmp = [TreeNode(-float('inf')), None, None] self.rec(root, tmp) tmp[1].val, tmp[2].val = tmp[2].val, tmp[1].val return BinaryTree(root).inorder()
""" ans = [] node, stack, tot, path = root, [], 0, [] while node or stack: if node: tot += node.val path.append(node.val) if not node.left and not node.right: if tot == sum: ans.append(path) node = None else: if node.right: stack.append((tot, node.right, list(path))) node = node.left else: tot, node, path = stack.pop() return ans if __name__ == "__main__": from leetcodelib import test, update_testfile, run_testfile nums = [5, 4, 8, 11, None, 13, 4, 7, 2, None, None, 5, 1] arguments = [(BinaryTree(nums).root, 22)] answers = [[[5, 4, 11, 2], [5, 8, 4, 5]]] test(Solution2().pathSum, arguments, answers, mode='1D') #testfile = __file__.replace('.py', '.yaml') #arg_names = "" #update_testfile(testfile, arg_names, arguments, answers) #run_testfile(testfile, Solution().)
:type root: TreeNode :type k: int :rtype: int """ if not root or k == 0: return None node, stack = root, [] while node or stack: if node: stack.append(node) node = node.left else: node = stack.pop() k -= 1 if k == 0: return node.val node = node.right if __name__ == "__main__": from leetcodelib import test, update_testfile, run_testfile nums = [[1, None, 2], [3, 1, 4, None, 2]] ks = [2, 2] arguments = [(BinaryTree(nums[i]).root, ks[i]) for i in xrange(len(ks))] answers = [2, 2] test(Solution().kthSmallest, arguments, answers, inds=[1]) #testfile = __file__.replace('.py', '.yaml') #arg_names = "" #update_testfile(testfile, arg_names, arguments, answers) #run_testfile(testfile, Solution().)
class Solution3(object): def flatten(self, root): """ :type root: TreeNode :rtype: void Do not return anything, modify root in-place instead. """ self.rec(root, None) return root def rec(self, root, prev): if not root: return prev prev = self.rec(root.right, prev) prev = self.rec(root.left, prev) root.right = prev root.left = None prev = root return prev if __name__ == "__main__": from leetcodelib import test, update_testfile, run_testfile nums = [1,2,5,3,4,None,6] #nums = [1, 2] arguments = [BinaryTree(nums).root] answers = [sorted([x for x in nums if x != None])] test(Solution3().flatten, arguments, answers, mode='inorder') #testfile = __file__.replace('.py', '.yaml') #arg_names = "" #update_testfile(testfile, arg_names, arguments, answers) #run_testfile(testfile, Solution().)
:rtype: List[List[int]] """ if not root: return [] level, ans, direction = [root], [], -1 while level: tmp, values = [], [] for i in xrange(len(level)): values.append(level[i].val) children = [level[~i].left, level[~i].right] for child in children[::direction]: if child: tmp.append(child) ans.append(values) level = tmp direction = -direction return ans if __name__ == "__main__": from leetcodelib import test, update_testfile, run_testfile nums = [[3, 9, 20, None, None, 15, 7], [1, 2, 3, 4, None, None, 5]] arguments = [BinaryTree(x).root for x in nums] answers = [[[3], [20, 9], [15, 7]], [[1], [3, 2], [4, 5]]] test(Solution().zigzagLevelOrder, arguments, answers) #testfile = __file__.replace('.py', '.yaml') #arg_names = "" #update_testfile(testfile, arg_names, arguments, answers) #run_testfile(testfile, Solution().)
class Solution2(object): def isValidBST(self, root): """ :type root: TreeNode :rtype: bool """ return self.rec(root) def rec(self, root, minval=-float('inf'), maxval=float('inf')): if not root: return True if root.val <= minval or root.val >= maxval: return False return self.rec(root.left, minval, root.val) and self.rec( root.right, root.val, maxval) if __name__ == "__main__": from leetcodelib import test, update_testfile, run_testfile nums = [[4, 2, 6, 1, 3, 5, 7], [4, 2, 5, 3, 1, 6, 7], [4, 2, 5, 1, 3, 7, 6], [10, 5, 15, None, None, 6, 20]] arguments = [BinaryTree(num).root for num in nums] answers = [True, False, False, False] test(Solution2().isValidBST, arguments, answers, inds=None) #testfile = __file__.replace('.py', '.yaml') #arg_names = "" #update_testfile(testfile, arg_names, arguments, answers) #run_testfile(testfile, Solution().)
def test_binary_tree(): import time start_time = time.time() # Insert test functions below import numpy as np numbers = [2, 1, 3, 0, 7, 9, 1, 2, None, 1, 0, None, None, 8, 8, None, None, None, None, 7] # tree1 # 2 # 1 3 # 0 7 9 1 # 2 1 0 8 8 # 7 tree1 = BinaryTree(numbers) tree1.write() if tree1.values() != numbers: print "generated linked list values not equal to original numbers" inorder = [2, 0, 1, 1, 7, 7, 0, 2, 9, 3, 8, 1, 8] print "inorder", inorder print "check inorder", tree1.inorder() == inorder print "check morris_inorder", tree1.morris_inorder() == inorder preorder = [2, 1, 0, 2, 7, 1, 0, 7, 3, 9, 1, 8, 8] print "preorder", preorder print "check preorder", tree1.preorder() == preorder print "check morris_preorder", tree1.morris_preorder() == preorder postorder = [2, 0, 1, 7, 0, 7, 1, 9, 8, 8, 1, 3, 2] print "postorder", postorder print "check postorder", tree1.postorder() == postorder tree2 = BinaryTree(tree1.root.left) tree2.write() BinaryTree().write(tree1.root.left) print("Tests finished in %s seconds" % (time.time() - start_time))
def isSameTree(self, p, q): """ :type p: TreeNode :type q: TreeNode :rtype: bool """ if not p and not q: return True elif p and q: return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) else: return False if __name__ == "__main__": from leetcodelib import test, update_testfile, run_testfile nums1 = [4, 2, 6, 1, 3, 5, 7] nums2 = [4, 2, 6, 1, None, 5, 7] nums3 = [4, 2, 6, None, 1, 5, 7] tree1 = BinaryTree(nums1) tree2 = BinaryTree(nums2) tree3 = BinaryTree(nums3) arguments = [(tree1.root, tree1.root), (tree1.root, tree2.root), (tree2.root, tree3.root)] answers = [True, False, False] test(Solution().isSameTree, arguments, answers) #testfile = __file__.replace('.py', '.yaml') #arg_names = "" #update_testfile(testfile, arg_names, arguments, answers) #run_testfile(testfile, Solution().)
stack.append((node, i, ind)) if j > ind + 1: node.right = TreeNode(postorder.pop()) node = node.right i = ind + 1 ind = post2in.pop() elif stack: node, i, j = stack.pop() node.left = TreeNode(postorder.pop()) node = node.left ind = post2in.pop() else: return root if __name__ == "__main__": from leetcodelib import test, update_testfile, run_testfile #nums = [2, 1, 3, 4, 7, 9, 10, 5, None, 6, 11, None, None, 12, 13, None, None, None, None, 14] nums = [1, 2] tree = BinaryTree(nums) arguments = [(tree.inorder(), tree.postorder())] answers = [nums] test(Solution2().buildTree, arguments, answers) #arguments = [] #answers = [] #test(Solution()., arguments, answers) #testfile = __file__.replace('.py', '.yaml') #arg_names = "" #update_testfile(testfile, arg_names, arguments, answers) #run_testfile(testfile, Solution().)
stack.append((node.right, h + 1)) stack.append((node, h)) node, h = node.left, h + 1 else: node, h = stack.pop() if stack and node.right == stack[-1][0]: stack.pop() stack.append((node, h)) node, h = node.right, h + 1 else: #print "tree", node.val, 'height', h if h > height: count += 1 elif not node.right: return count node, h = None, None #print count return count if __name__ == "__main__": from leetcodelib import test, update_testfile, run_testfile tree1 = BinaryTree([1, 2, 3, 4, 5]) arguments = [tree1.root] answers = [5] test(Solution().countNodes, arguments, answers) #testfile = __file__.replace('.py', '.yaml') #arg_names = "" #update_testfile(testfile, arg_names, arguments, answers) #run_testfile(testfile, Solution().)
if not root: return [] self.rec(root.left, ans) ans.append(root.val) self.rec(root.right, ans) def ite(self, root, ans): node, stack = root, [] while node or stack: if node: stack.append(node) node = node.left else: node = stack.pop() ans.append(node.val) node = node.right return ans if __name__ == "__main__": from leetcodelib import test, update_testfile, run_testfile from random import sample nums = [2, 1, 3, 0, 7, 9, 1, 2, None, 1, 0, None, None, 8, 8, None, None, None, None, 7] tree = BinaryTree(nums) arguments = [tree.root] answers = [tree.inorder()] test(Solution().inorderTraversal, arguments, answers) #testfile = __file__.replace('.py', '.yaml') #arg_names = "" #update_testfile(testfile, arg_names, arguments, answers) #run_testfile(testfile, Solution().)