def test2(): n1 = BTNode(1) n2 = BTNode(2) n1.left = n2 print(pathSum(n1, 0, 1)) print(pathSumAlt(n1, 1))
def test2(): #rslt = buildUniqueBST(4) rslt = buildBST_dp(4) for t in rslt: BTNode.print_nodes(t) print('--------------') print('==============')
def test2(): n1 = BTNode(1) n2 = BTNode(2) n1.left = n2 print(pathSum(n1, 0, 1)) print(pathSumAlt(n1, 1))
def test2(): #rslt = buildUniqueBST(4) rslt = buildBST_dp(4) for t in rslt: BTNode.print_nodes(t) print('--------------') print('==============')
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 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 test2(): post_o = [2, 1] in_o = [2, 1] BTNode.print_nodes(buildT(post_o, in_o)) print('-----------------------------') post_o = [2, 1] in_o = [2, 1] BTNode.print_nodes(buildT(post_o, in_o))
def test1(): n5 = BTNode(5) n2 = BTNode(2) nm3 = BTNode(-3) n5.left = n2 n5.right = nm3 print(mfss(n5))
def test2(): post_o = [2, 1] in_o = [2, 1] BTNode.print_nodes(buildT(post_o, in_o)) print('-----------------------------') post_o = [2, 1] in_o = [2, 1] BTNode.print_nodes(buildT(post_o, in_o))
def test2(): n5 = BTNode(5) n2 = BTNode(2) nm5 = BTNode(-5) n5.left = n2 n5.right = nm5 print(mfss(n5))
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(): rslt = buildUniqueBST(3) for t in rslt: BTNode.print_nodes(t) print('--------------') print('==============') rslt = buildBST_dp(3) for t in rslt: BTNode.print_nodes(t) print('--------------') print('==============')
def test1(): rslt = buildUniqueBST(3) for t in rslt: BTNode.print_nodes(t) print('--------------') print('==============') rslt = buildBST_dp(3) for t in rslt: BTNode.print_nodes(t) print('--------------') print('==============')
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 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 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 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 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(): 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 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 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 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) n1.left = n2 n2.left = n3 flatten(n1) BTNode.print_nodes(n1) print('--------------------------')
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 test1(): n1 = BTNode(1) n2 = BTNode(2) n5 = BTNode(5) n7 = BTNode(7) n9 = BTNode(9) n8 = BTNode(8) n1.left = n2 n2.left = n5 n2.right = n9 n5.left = n7 n9.left = n8 print(rightView(n1))
def test3(): n1 = BTNode(1) n2 = BTNode(2) n3 = BTNode(3) n4 = BTNode(4) n5 = BTNode(5) n7 = BTNode(7) n1.left = n2 n2.left = n3 n3.left = n4 n4.left = n5 n5.left = n7 rslt = [] inOrderTrav(n1, rslt) print(rslt) print(inOrderTravRec(n1)) print("--------------")
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 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 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 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 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))
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) n1.left = n2 n2.right = n3 n3.right = n4 n4.left = n5 lst = [] lot(n1, 0, lst) print(lst)
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 test1(): n5 = BTNode(5) n2 = BTNode(2) nm3 = BTNode(-3) n5.left = n2 n5.right = nm3 print(mfss(n5))
def test2(): n5 = BTNode(5) n2 = BTNode(2) nm5 = BTNode(-5) n5.left = n2 n5.right = nm5 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 test3(): l1 = ListNode(1) sol = LinkedListToTree() BTNode.print_nodes(sol.ll2tfaster(l1)) print('-----------------') sol = LinkedListToTree2() BTNode.print_nodes(sol.ll2tfaster2(l1)) print('-----------------') BTNode.print_nodes(ll2t(l1)) print('====================')
def test1(): n20 = BTNode(20) n9 = BTNode(9) n25 = BTNode(25) n5 = BTNode(5) n12 = BTNode(12) n10 = BTNode(10) n14 = BTNode(14) n20.left = n9 n20.right = n25 n9.left = n5 n9.right = n12 n12.left = n10 n12.right = n14 print(findSmallestBSTKey_itr(n20, 17)) print(findSmallestBSTKey(n20, 17)) print(findSmallestBSTKey_itr(n20, 12)) print(findSmallestBSTKey(n20, 12)) print(findSmallestBSTKey_itr(n20, 14)) print(findSmallestBSTKey(n20, 14))
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 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 test3(): l1 = ListNode(1) sol = LinkedListToTree() BTNode.print_nodes(sol.ll2tfaster(l1)) print('-----------------') sol = LinkedListToTree2() BTNode.print_nodes(sol.ll2tfaster2(l1)) print('-----------------') BTNode.print_nodes(ll2t(l1)) print('====================')
def test4(): l1 = ListNode(1) l2 = ListNode(2) l1.next = l2 sol = LinkedListToTree() BTNode.print_nodes(sol.ll2tfaster(l1)) print('-----------------') sol = LinkedListToTree2() BTNode.print_nodes(sol.ll2tfaster2(l1)) print('-----------------') BTNode.print_nodes(ll2t(l1)) print('====================')
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 test4(): l1 = ListNode(1) l2 = ListNode(2) l1.next = l2 sol = LinkedListToTree() BTNode.print_nodes(sol.ll2tfaster(l1)) print('-----------------') sol = LinkedListToTree2() BTNode.print_nodes(sol.ll2tfaster2(l1)) print('-----------------') BTNode.print_nodes(ll2t(l1)) print('====================')
def test1(): l1 = ListNode(1) l2 = ListNode(2) l3 = ListNode(3) l4 = ListNode(4) l5 = ListNode(5) l6 = ListNode(6) l1.next = l2 l2.next = l3 l3.next = l4 l4.next = l5 l5.next = l6 sol = LinkedListToTree() BTNode.print_nodes(sol.ll2tfaster(l1)) print('-----------------') sol = LinkedListToTree2() BTNode.print_nodes(sol.ll2tfaster2(l1)) print('-----------------') BTNode.print_nodes(ll2t(l1)) print('====================')
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 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 test2(): n1 = BTNode(1) n2 = BTNode(2) n1.left = n2 print(isBST(n1))
def test1(): n1 = BTNode(1) print(isBST(n1))
def test3(): n1 = BTNode(1) print(mfss(n1))
def test4(): n4 = BTNode(4) n3 = BTNode(2) n4.left = n3 print(is_weight_balanced(n4))
def test3(): n4 = BTNode(4) print(is_weight_balanced(n4))
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(): nums = [1, 2, 3, 4, 5, 6, 7, 8] BTNode.print_nodes(a2t(nums))