标签:child width alt pop append 复杂 app tree lse
def preorder(tree): if tree: print(tree.val) preorder(tree.getLeftChild()) preorder(tree.getRightChild())
def preOrder(head): if not head: return None res ,stack = [] , [head] while stack: cur = stack.pop() res.append(cur.val) if cur.right: stack.append(cur.right) if cur.left: stack.append(cur.left) return res
def inorder(tree): if tree: inorder(tree.left) print(tree.val) inorder(tree.right)
def InOrder(head): if not head: return None res ,stack = [] , [] cur = head while stack or cur: if cur: stack.append(cur) cur = cur.left else: cur = stack.pop() res.append(cur.val) cur =cur.right return res
def postorder(tree): if tree: postorder(tree.left) postorder(tree.right)) print(tree.val)
def PosOrder(head): if not head: return [] s1 , s2 = [head] , [] cur = head while s1: cur = s1.pop() s2.append(cur.val) if cur.left: s1.append(cur.left) if cur.right: s1.append(cur.right) return s2[::-1]
标签:child width alt pop append 复杂 app tree lse
原文地址:https://www.cnblogs.com/Lee-yl/p/9809835.html