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 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(): 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) n10 = BTNode(10) n11 = BTNode(11) n1.left = n2 n1.right = n3 n2.left = n4 n2.right = n5 n4.left = n6 n4.right = n7 n3.left = n8 n3.right = n9 n8.left = n10 n8.right = n11 print(pot(n1)) print(pot_stack(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 test3(): n1 = BTNode(1) n2 = BTNode(2) n3 = BTNode(3) n1.right = n2 n2.right = n3 flatten(n1) BTNode.print_nodes(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) n3 = BTNode(3) n4 = BTNode(4) n1.left = n2 n2.right = n4 n1.right = n3 print(constructStr(n1)) print(constructStr2_top(n1))
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(): 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 test1(): n1 = BTNode(1) n2 = BTNode(2) n3 = BTNode(3) n4 = BTNode(4) n5 = BTNode(5) n6 = BTNode(6) n3.left = n2 n3.right = n5 n2.left = n1 n5.left = n4 n5.right = n6 print(isBalBST(n3))
def test2(): n1 = BTNode(1) n2 = BTNode(2) n3 = BTNode(3) n4 = BTNode(4) n5 = BTNode(5) n6 = BTNode(6) n7 = BTNode(7) n1.left = n2 n1.right = n3 n2.left = n4 n2.right = n5 n3.left = n6 n3.right = n7 print(sumRoot2LeafNumbers(n1))
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 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 test1(): n4 = BTNode(4) n2 = BTNode(2) n1 = BTNode(1) n3 = BTNode(3) n7 = BTNode(7) n6 = BTNode(6) n9 = BTNode(9) n4.left = n2 n4.right = n7 n2.left = n1 n2.right = n3 n7.left = n6 n7.right = n9 invertT(n4) BTNode.print_nodes(n4)
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 test1(): n1 = BTNode(1) n2 = BTNode(2) n3 = BTNode(3) n4 = BTNode(4) n5 = BTNode(5) n7 = BTNode(7) n1.left = n2 n1.right = n3 n2.left = n4 n2.right = n5 n5.right = n7 rslt = [] inOrderTrav(n1, rslt) print(rslt) print(inOrderTravRec(n1)) print("--------------")
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(): 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 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 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 test1(): n1 = BTNode(1) n2 = BTNode(2) n3 = BTNode(3) n4 = BTNode(4) n5 = BTNode(5) n6 = BTNode(6) n4.left = n6 n6.left = n1 n6.right = n3 n4.right = n5 n5.right = n2 rt = RT() BTNode.print_nodes(n4) print('---------------') rt.rt(n4) BTNode.print_nodes(n4) print('==============')
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) n1.left = n2 n2.left = n3 n2.right = n4 n1.right = n6 n6.left = n5 n6.right = n8 n8.left = n7 print(postOrderT(n1)) print(postOrderT_r(n1)) print(postOrderT_Alt_r(n1))
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 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 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 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_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 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 test2(): n11 = BTNode(11) n8 = BTNode(8) n4 = BTNode(4) n5 = BTNode(5) n6 = BTNode(6) n7 = BTNode(7) n11.left = n8 n8.left = n4 n4.right = n5 n5.right = n6 n6.right = n7 res = inorderBSTSuccessor(n11, n11) val = None if not res else res.val print(val) print('----------') res = inorderBSTSuccessor(n11, n7) val = None if not res else res.val print(val)
def test1(): n11 = BTNode(11) n8 = BTNode(8) n4 = BTNode(4) n1 = BTNode(1) n5 = BTNode(5) n6 = BTNode(6) n7 = BTNode(7) n16 = BTNode(16) n14 = BTNode(14) n15 = BTNode(15) n12 = BTNode(12) n13 = BTNode(13) n11.left = n8 n8.left = n4 n4.left = n1 n4.right = n5 n5.right = n6 n6.right = n7 n11.right = n16 n16.left = n14 n14.left = n12 n14.right = n15 n12.right = n13 res = inorderBSTSuccessor(n11, n7) val = None if not res else res.val print(val) print('----------') res = inorderBSTSuccessor(n11, n12) val = None if not res else res.val print(val) print('=============')
def test1(): n7 = BTNode(7) n3 = BTNode(3) n1 = BTNode(1) n0 = BTNode(0) n2 = BTNode(2) n5 = BTNode(5) n4 = BTNode(4) n12 = BTNode(12) n10 = BTNode(10) n8 = BTNode(8) n9 = BTNode(9) n11 = BTNode(11) n15 = BTNode(15) n7.left = n3 n3.left = n1 n1.left = n0 n1.right = n2 n3.right = n5 n5.left = n4 n7.right = n12 n12.left = n10 n10.left = n8 n8.right = n9 n10.right = n11 n12.right = n15 i, v = BSTIterator(n7), [] while i.hasNext(): v.append(i.next()) print(v)
def test2(): n1 = BTNode(1) n2l = BTNode(2) n2r = BTNode(2) n3l = BTNode(3) n7l = BTNode(7) n7r = BTNode(7) n3r = BTNode(3) n4l = BTNode(4) n5l = BTNode(5) n6l = BTNode(6) n6r = BTNode(6) n5r = BTNode(5) n6 = BTNode(6) n1.left = n2l n1.right = n2r n2l.left = n3l n2l.right = n7l n2r.left = n7r n2r.right = n3r n3l.left = n4l n3l.right = n5l n7l.left = n6l n7r.right = n6r n3r.left = n5l n3r.right = n6 print(isSymmetric(n1))
def test4(): n1a = BTNode(1) n1b = BTNode(1) n2a = BTNode(2) n2b = BTNode(2) n3a = BTNode(3) n3b = BTNode(3) n4a = BTNode(4) n5b = BTNode(5) n1a.left = n2a n2a.left = n3a n2a.right = n4a n1b.left = n2b n2b.left = n3b n2b.right = n5b print(isSameT(n1a, n1b))