def main(): n = binary_tree.array2tree([10, 6, 14, 4, 8, 12, 16]) a, d = convert(n) tmp = a while tmp: print tmp.data tmp = tmp.right
def main(): node = array2bst([1,2,3,4,5,6,7,8,9]) print is_bst1(node) print is_bst2(node) node = array2tree([1,2,3,4,5,6,7,8,9]) print is_bst1(node) print is_bst2(node)
def main(): node = array2bst([1, 2, 3, 4, 5, 6, 7, 8, 9]) print is_bst1(node) print is_bst2(node) node = array2tree([1, 2, 3, 4, 5, 6, 7, 8, 9]) print is_bst1(node) print is_bst2(node)
def main(): root = array2tree([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) res = bfs(root) keys = res.keys() keys.sort() for k in keys: print k, for i in res[k]: print i.data, print ""
def main(): root = array2tree([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) res = bfs(root) keys = res.keys() keys.sort() for k in keys: print 'L%d: ' % k, for i in res[k]: print i.data, print ""
return False stack1 = [p] stack2 = [q] while stack1: p = stack1.pop() q = stack2.pop() if not p and not q: continue if not p.val == q.val: return False if p.left and q.left: stack1.append(p.left) stack2.append(q.left) elif p.right or q.right: return False if p.right and q.right: stack1.append(p.right) stack2.append(q.right) elif p.right or q.right: return False return True if __name__ == "__main__": s = Solution() p = array2tree([1, None, 1]) q = array2tree([1, None, 1]) print s.isSameTree(p, q)
return False stack1 = [p] stack2 = [q] while stack1: p = stack1.pop() q = stack2.pop() if not p and not q: continue if not p.val == q.val: return False if p.left and q.left: stack1.append(p.left) stack2.append(q.left) elif p.right or q.right: return False if p.right and q.right: stack1.append(p.right) stack2.append(q.right) elif p.right or q.right: return False return True if __name__ == '__main__': s = Solution() p = array2tree([1, None, 1]) q = array2tree([1, None, 1]) print s.isSameTree(p, q)
def main(): node = binary_tree.array2tree([10, 5, 12, 4, 7, 10]) for i in find(node, 22, 22): print i
def main(): l = binary_tree.array2tree([1, 2, 3, 4, 5, 6, 7, 8, 9], True) print l[7].data print l[4].data print common_ancestor(l[7], l[4])
def main(): a = array2tree([1, 2, 3, 4, 5, 6, 7]) certain_level(a, 2)
""" implement next (in-order) """ import os import sys sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from structs import binary_tree if __name__ == "__main__": l = binary_tree.array2tree([1, 2, 3, 4, 5, 6, 7, 8, 9], True) node = l[-2] while node: print node.data, node = node.in_order_next() print "" for i in l[0].in_order(): print i,
if node.left: stack.append(node.left) stack.append(node) else: print node.data, def post_order(root): stack = [] stack.append(root) while stack: node = stack.pop() if not hasattr(node, 'visit'): node.visit = True stack.append(node) if node.right: stack.append(node.right) if node.left: stack.append(node.left) else: print node.data, if __name__ == '__main__': node = array2tree(range(14)) for i in node.post_order(): print i, print '' node = array2tree(range(14)) post_order(node)
def main(): l = binary_tree.array2tree([1,2,3,4,5,6,7,8,9], True) print l[7].data print l[4].data print common_ancestor(l[7], l[4])
""" implement next (in-order) """ import os import sys sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from structs import binary_tree if __name__ == '__main__': l = binary_tree.array2tree([1, 2, 3, 4, 5, 6, 7, 8, 9], True) node = l[-2] while node: print node.data, node = node.in_order_next() print "" for i in l[0].in_order(): print i,