def test2(): n1 = BTNode(1) n2 = BTNode(2) n1.left = n2 print(pathSum(n1, 0, 1)) print(pathSumAlt(n1, 1))
def test1(): n3 = BTNode(3) n5 = BTNode(5) n6 = BTNode(6) n2 = BTNode(2) n7 = BTNode(7) n4 = BTNode(4) n1 = BTNode(1) n0 = BTNode(0) n8 = BTNode(8) n3.left = n5 n5.left = n6 n3.right = n1 n5.right = n2 n2.left = n7 n2.right = n4 n1.left = n0 n1.right = n8 n_nonvalid = BTNode(13) print(lca(n3, n6, n2).val) print(lca_non_valid_node(n3, n6, n_nonvalid)) print(lca_non_valid_node(n3, n6, n2)) print(lca_non_valid_node(n3, n4, n2))
def test1(): n15 = BTNode(15) n7 = BTNode(7) n30 = BTNode(30) n11 = BTNode(11) n9 = BTNode(9) n8 = BTNode(8) n10 = BTNode(10) n14 = BTNode(14) n13 = BTNode(13) n12 = BTNode(12) n15.left = n7 n15.right = n30 n7.right = n11 n11.left = n9 n11.right = n14 n9.left = n8 n9.right = n10 n11.right = n14 n14.left = n13 n13.left = n12 print(lcaBST(n15, n8, n12).val) print(lcaBST(n15, n13, n12).val) print(lcaBST(n15, n7, n12).val)
def test2(): n5 = BTNode(5) n2 = BTNode(2) nm5 = BTNode(-5) n5.left = n2 n5.right = nm5 print(mfss(n5))
def test1(): n5 = BTNode(5) n2 = BTNode(2) nm3 = BTNode(-3) n5.left = n2 n5.right = nm3 print(mfss(n5))
def test3(): n1 = BTNode(1) n2 = BTNode(2) n3 = BTNode(3) n1.right = n2 n2.right = n3 flatten(n1) BTNode.print_nodes(n1)
def test2(): n1 = BTNode(1) n2 = BTNode(2) n3 = BTNode(3) n4 = BTNode(4) n1.left = n2 n2.left = n3 n3.left = n4 print(isBalBST(n1))
def test1(): n3 = BTNode(3) n9 = BTNode(9) n20 = BTNode(20) n15 = BTNode(15) n7 = BTNode(7) n3.left = n9 n3.right = n20 n20.left = n15 n20.right = n7 print(zzTrav(n3))
def test2(): n1 = BTNode(1) n2 = BTNode(2) n2.right = n1 rt = RT() BTNode.print_nodes(n2) print('---------------') rt.rt(n2) BTNode.print_nodes(n2)
def test2(): n1 = BTNode(1) n2 = BTNode(2) n3 = BTNode(3) n1.left = n2 n2.left = n3 flatten(n1) BTNode.print_nodes(n1) print('--------------------------')
def test3(): n5 = BTNode(5) n4 = BTNode(4) n9 = BTNode(9) n7 = BTNode(7) n2 = BTNode(2) n5.left = n4 n4.left = n9 n9.left = n7 n5.right = n2 print(sumRoot2LeafNumbers(n5))
def test2(): n1 = BTNode(1) n2 = BTNode(2) n3 = BTNode(3) n4 = BTNode(4) n1.right = n2 n2.right = n3 n3.right = n4 print(postOrderT(n1)) print(postOrderT_r(n1)) print(postOrderT_Alt_r(n1))
def test1(): n7 = BTNode(7) n10 = BTNode(10) n4 = BTNode(4) n3 = BTNode(3) n1 = BTNode(1) n2 = BTNode(2) n8 = BTNode(8) n11 = BTNode(11) n7.left = n10 n7.right = n2 n10.left = n4 n10.right = n3 n3.right = n1 n2.left = n8 n8.left = n11 BTNode.print_nodes(n7) print('-----------------------------') post_o = [4, 1, 3, 10, 11, 8, 2, 7] in_o = [4, 10, 3, 1, 7, 11, 8, 2] BTNode.print_nodes(buildT(post_o, in_o)) print('-----------------------------') post_o = [4, 1, 3, 10, 11, 8, 2, 7] in_o = [4, 10, 3, 1, 7, 11, 8, 2] BTNode.print_nodes(buildTFaster(post_o, in_o)) print('-----------------------------')
def test1(): n3 = BTNode(3) n9 = BTNode(9) n20 = BTNode(20) n15 = BTNode(15) n7 = BTNode(7) n3.left = n9 n3.right = n20 n20.left = n15 n20.right = n7 lst = [] lot(n3, 0, lst) print(lst)
def test2(): n1 = BTNode(1) n2 = BTNode(2) n3 = BTNode(3) n4 = BTNode(4) n5 = BTNode(5) n1.left = n2 n2.right = n3 n3.right = n4 n4.left = n5 lst = [] lot(n1, 0, lst) print(lst)
def a2t(nums): if not nums: return None beg, end = 0, len(nums) mid = beg + (end-beg)/2 n = BTNode(nums[mid]) n.left = a2t(nums[:mid]) n.right = a2t(nums[mid+1:]) return n
def buildTHelper(post_o, in_o, idx_dic, beg, end): if beg < end: mid = idx_dic[post_o.pop()] n = BTNode(in_o[mid]) n.right = buildTHelper(post_o, in_o, idx_dic, mid + 1, end) n.left = buildTHelper(post_o, in_o, idx_dic, beg, mid) return n
def constructTree(arr): if not arr: return None idx = findMax(arr) n = BTNode(arr[idx]) lhs = constructTree(arr[:idx]) rhs = constructTree(arr[idx + 1:]) n.left = lhs n.right = rhs return n
def buildT(post_o, in_o): """ 68% percentile in leetcode """ if in_o: mid = in_o.index(post_o.pop()) n = BTNode(in_o[mid]) n.right = buildT(post_o, in_o[mid + 1:]) n.left = buildT(post_o, in_o[:mid]) return n
def ll2t(head): """ Accepted first try 66.6% peformance """ if not head: return None if not head.next: return BTNode(head.val) fast = slow = head if fast.next and fast.next.next: fast = fast.next.next while fast.next and fast.next.next: slow = slow.next fast = fast.next.next mid = slow.next n = BTNode(mid.val) slow.next = None n.left = ll2t(head) n.right = ll2t(mid.next) return n
def ll2tfaster_dfs2(self, beg, end): if beg > end: return None mid = beg + (end - beg) / 2 lhs = self.ll2tfaster_dfs2(beg, mid - 1) tn = BTNode(self.node.val) tn.left = lhs self.node = self.node.next rhs = self.ll2tfaster_dfs2(mid + 1, end) tn.right = rhs return tn
def ll2tfaster_dfs(self, n): # n/2: left side length n/2 # n-n/2-1: right_side_len: total_len - lft_len - mid # note: lhs = n/2-1, rhs = n-n/2 does not work # e.g. [1, 2, 3] if n == 0: return None lhs = self.ll2tfaster_dfs(n / 2) tn = BTNode(self.node.val) self.node = self.node.next rhs = self.ll2tfaster_dfs(n - n / 2 - 1) tn.left = lhs tn.right = rhs return tn
def test1(): n4 = BTNode(4) n1 = BTNode(1) n15 = BTNode(15) n3 = BTNode(3) n0 = BTNode(0) n2 = BTNode(2) n1a = BTNode(1) n1b = BTNode(1) n7 = BTNode(7) n15 = BTNode(15) n4.left = n1 n4.right = n15 n1.left = n3 n1.right = n7 n3.left = n0 n3.right = n2 n0.left = n1a n0.right = n1b n4.right = n15 print(is_weight_balanced(n4))
def test1(): n5a = BTNode(5) n4a = BTNode(4) n8 = BTNode(8) n11 = BTNode(11) n7 = BTNode(7) n2 = BTNode(2) n13 = BTNode(13) n4b = BTNode(4) n5b = BTNode(5) n1 = BTNode(1) n5a.left = n4a n4a.left = n11 n11.left = n7 n11.right = n2 n5a.right = n8 n8.left = n13 n8.right = n4b n4b.left = n5b n4b.right = n1 print(pathSum(n5a, 22))
def test2(): n4 = BTNode(4) n1 = BTNode(1) n7 = BTNode(7) n3a = BTNode(3) n3b = BTNode(3) n0a = BTNode(0) n0b = BTNode(0) n0c = BTNode(0) n0d = BTNode(0) n4.left = n1 n4.right = n7 n1.left = n3a n1.right = n3b n3a.left = n0a n3a.right = n0b n0a.left = n0c n0c.left = n0d print(is_weight_balanced(n4))
def test1(): nm1 = BTNode(-1) nm2 = BTNode(-2) nm3 = BTNode(-3) nm4 = BTNode(-4) nm5 = BTNode(-5) nm6 = BTNode(-6) n12 = BTNode(12) n15 = BTNode(15) nm1.left = nm2 nm1.right = n12 nm2.right = nm5 n12.right = nm4 nm4.left = n15 nm4.right = nm2 nm2.left = nm6 print(maxSumPath(nm1))
def test3(): n9 = BTNode(9) n5 = BTNode(5) n13 = BTNode(13) n3 = BTNode(3) n2 = BTNode(2) n4 = BTNode(4) n11 = BTNode(11) n21 = BTNode(21) n9.left = n5 n9.right = n13 n5.left = n3 n3.left = n2 n3.right = n4 n13.left = n11 n13.right = n21 print(isBST(n9))
def test1(): n5 = BTNode(5) n4 = BTNode(4) n8 = BTNode(8) n11 = BTNode(11) n13 = BTNode(13) n4b = BTNode(4) n1 = BTNode(1) n7 = BTNode(7) n2 = BTNode(2) n5.left = n4 n4.left = n11 n5.right = n8 n8.left = n13 n8.right = n4 n4b.right = n1 n4.left = n11 n11.left = n7 n11.right = n2 print(pathSum(n5, 0, 22)) print(pathSumAlt(n5, 22))
def test1(): n1 = BTNode(1) n2 = BTNode(2) n3 = BTNode(3) n4 = BTNode(4) n5 = BTNode(5) n6 = BTNode(6) n7 = BTNode(7) n8 = BTNode(8) n9 = BTNode(9) n1.left = n2 n2.left = n3 n2.right = n4 n4.left = n5 n4.right = n6 n1.right = n7 n7.left = n8 n7.right = n9 flatten(n1) BTNode.print_nodes(n1) print('--------------------------')
def test1(): n1 = BTNode(1) n3 = BTNode(3) n5 = BTNode(5) n9 = BTNode(9) n7 = BTNode(7) n6 = BTNode(6) n8 = BTNode(8) n13 = BTNode(13) n11 = BTNode(11) n5.left = n3 n3.left = n1 n5.right = n9 n9.left = n7 n7.left = n6 n7.right = n8 n9.right = n13 n13.left = n11 res = dfs(n5, [0], 8) print(res) print(kSmallestIter(n5, 8))