1. First Order traversal: the right subtree, left dial hand tree, root node
1 # Sequential Print binary tree (recursive) 2 def preordertraverse (node): 3 if is None: 4 return None 5 Print (Node.val) 6 preordertraverse (node.left)7 preordertraverse (node.right)
1 #Sequential Print binary tree (non-recursive)2 defPreordertravese (node):3stack =[node]4 whileLen (Stack) >0:5 Print(Node.val)6 ifNode.right is notNone:7 stack.append (node.right)8 ifNode.left is notNone:9 stack.append (node.left)Tennode = Stack.pop ()
2. Middle Sequence traversal: the right subtree, the root node, left dial hand tree
1 # middle order Print binary tree (recursive) 2 def inordertraverse (node): 3 if is None: 4 return None 5 inordertraverse (node.left)6 Print (Node.val) 7 Inordertraverse (Node.right)
1 #middle order Print binary tree (non-recursive)2 definordertraverse (node):3stack =[node]4pos =node5 whilePos is notNoneorLen (Stack) >0:6 ifPos is notNone:7 stack.append (POS)8pos =Pos.left9 Else:Tenpos =Stack.pop () One Print(Pos.val) Apos = Pos.right
3. Post-post traversal: root node, right subtree, left dial hand tree
1 # Post -Secondary print binary tree (recursive) 2 def postordertraverse (node): 3 if is None: 4 return None 5 postordertraverse (node.left)6 postordertraverse (node.right)7 Print(node.val)
1 #Post -Secondary printing binary tree (non-recursive)2 #use two stack structures3 #first stack in stack order: left node, right node, and node4 #First Stack popup order: Left node, right node, and node (first sequence traversal stack popup order: left-to-right)5 #the second stack is stored for each popup in the first stack, followed by a stack6 #Finally, the second stack is out of the stack .7 defpostordertraverse (node):8stack =[node]9Stack2 = []Ten whileLen (Stack) >0: Onenode =Stack.pop () A stack2.append (node) - ifNode.left is notNone: - stack.append (node.left) the ifNode.right is notNone: - stack.append (node.right) - whileLen (Stack2) >0: - Print(Stack2.pop (). val)
4. Traverse by layer: from top to bottom, left to right, by layer
1 #Advanced first-out selection queue structure2 ImportQueue3 defLayertraverse (head):4 if notHead:5 returnNone6que = queue. Queue ()#Create a FIFO queue7 Que.put (head)8 while notque.empty ():9Head = Que.get ()#pops up the first element and printsTen Print(Head.val) One ifHead.left:#If the node has a left child node, join the queue (push the left node first) A que.put (head.left) - ifHead.right:#If the node has a right child node, join the queue (then push the right node) -Que.put (Head.right)
5. Number of two fork tree nodes
1 # finding the number of two-fork tree nodes 2 def treenodenums (node): 3 if is None: 4 return 0 5 Nums = treenodenums (node.left)6 nums + = treenodenums (node.right)7 return nums + 1
6. Maximum depth of two-fork tree
1 # two maximum depth of the fork tree 2 def btreedepth (node): 3 if is None: 4 return 0 5 Ldepth = btreedepth (node.left)6 rdepth = btreedepth (node.right)7 return (Max (ldepth, rdepth) + 1)
Recursive and non-recursive python implementations of binary tree First order, middle sequence, post-order traversal