1. Python实现
class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = Noneclass Solution: def __init__(self): self.res = [] # 先序遍历 def getPreOrder(self, root): # 递归法 def preOrderRec(root): if not root: return None # 根左右 self.res.append(root.val) preOrderRec(root.left) preOrderRec(root.right) # 迭代法 # 1) 弹出打印 # 2) 如有右,压入右 # 3) 如有左,压入左 def preOrderIter(root): if not root: self.res = [] stack = [root] while stack: root = stack.pop() self.res.append(root.val) if root.right: stack.append(root.right) if root.left: stack.append(root.left) # preOrderRec(root) preOrderIter(root) # 后续遍历 def getPostOrder(self, root): def postOrderRec(root): if not root: return None postOrderRec(root.left) postOrderRec(root.right) self.res.append(root.val) # 迭代法 # 1) 弹出打印 # 2) 如有左,压入、左 # 3) 如有右,压入右 def postOrderIter(root): if not root: self.res = [] stack = [root] while stack: self.res.append(root.val) root = stack.pop() if root.left: stack.append(root.left) if root.right: stack.append(root.right) # postOrderRec(root) postOrderIter(root) # 中序遍历 def getInOrder(self, root): def inOrderRec(root): if not root: return None inOrderRec(root.left) self.res.append(root.val) inOrderRec(root.right) # 迭代法 # 1) 整条左边界入栈 # 2) 如果 1) 无法进行,弹出打印 # 3) 右边界入栈 def inOrderIter(root): if not root: self.res = [] stack = [] while stack or root: if root: stack.append(root) root = root.left else: node = stack.pop() self.res.append(node.val) root = node.right # inOrderRec(root) inOrderIter(root) # 层序遍历 def bfs(self, root): if not root: self.res = [] queue = [root] while queue: nextLayer = [] for node in queue: self.res.append(node.val) if node.left: nextLayer.append(node.left) if node.right: nextLayer.append(node.right) queue = nextLayerif __name__ == "__main__": node1 = TreeNode('A') node2 = TreeNode('B') node3 = TreeNode('C') node4 = TreeNode('D') node5 = TreeNode('E') node6 = TreeNode('F') node7 = TreeNode('G') node8 = TreeNode('H') node9 = TreeNode('I') node1.left = node2 node1.right = node3 node2.left = node4 node2.right = node5 node3.left = node6 node3.right = node7 node4.left = node8 node4.right = node9 s = Solution() s.getPreOrder(node1) print (s.res) # ['A', 'B', 'D', 'H', 'I', 'E', 'C', 'F', 'G'] # s.getInOrder(node1) # print (s.res) # ['H', 'D', 'I', 'B', 'E', 'A', 'F', 'C', 'G'] # s.getPostOrder(node1) # print (s.res[::-1]) # ['H', 'I', 'D', 'E', 'B', 'F', 'G', 'C', 'A'] # s.bfs(node1) # print (s.res) # ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
2. Java实现
import java.util.*;/** * @Author dyliang * @Date 2020/10/7 21:22 * @Version 1.0 */public class BinaryTree { public static void main(String[] args) { TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(3); root.left.left = new TreeNode(4); root.left.right = new TreeNode(5); root.left.left.left = new TreeNode(8); root.left.left.right = new TreeNode(9); root.right.left = new TreeNode(6); root.right.right = new TreeNode(7); System.out.println("---------recursive---------"); preOrderRecur(root); System.out.println(); System.out.println("---------------------"); inOrderRecur(root); System.out.println(); System.out.println("---------------------"); postOrderRecur(root); System.out.println(); System.out.println("---------Iteration---------"); preOrderIter(root); System.out.println(); System.out.println("---------------------"); inOrderIter(root); System.out.println(); System.out.println("---------------------"); postOrderIter(root); System.out.println(); System.out.println("---------------------"); levelOrder(root); /** * ---------recursive--------- * 1 2 4 8 9 5 3 6 7 * --------------------- * 8 4 9 2 5 1 6 3 7 * --------------------- * 8 9 4 5 2 6 7 3 1 * ---------Iteration--------- * 1 2 4 8 9 5 3 6 7 * --------------------- * 8 4 9 2 5 1 6 3 7 * --------------------- * 8 9 4 5 2 6 7 3 1 * --------------------- * 1 2 3 4 5 6 7 8 */ } public static class TreeNode{ int val; TreeNode left; TreeNode right; public TreeNode(int val) { this.val = val; } public TreeNode() { } } /** * 递归方式的先序遍历 * @param root */ public static void preOrderRecur(TreeNode root){ if(root == null){ return; } System.out.print(root.val + " "); preOrderRecur(root.left); preOrderRecur(root.right); } /** * 递归方式的中序遍历 * @param root */ public static void inOrderRecur(TreeNode root){ if(root == null){ return; } inOrderRecur(root.left); System.out.print(root.val + " "); inOrderRecur(root.right); } /** * 递归方式的后续遍历 * @param root */ public static void postOrderRecur(TreeNode root){ if(root == null){ return; } postOrderRecur(root.left); postOrderRecur(root.right); System.out.print(root.val + " "); } /** * 迭代方式的先序遍历 * @param root */ public static void preOrderIter(TreeNode root){ if(root == null){ return; } Stack<TreeNode> stack = new Stack<>(); stack.push(root); while(!stack.isEmpty()){ TreeNode node = stack.pop(); System.out.print(node.val + " "); if(node.right != null){ stack.push(node.right); } if(node.left != null){ stack.push(node.left); } } } /** * 迭代方式的后序遍历 * @param root */ public static void postOrderIter(TreeNode root){ if(root == null){ return; } Stack<TreeNode> stack = new Stack<>(); List<Integer> list = new ArrayList<>(); stack.push(root); while(!stack.isEmpty()){ TreeNode node = stack.pop(); list.add(node.val); if(node.left != null){ stack.push(node.left); } if(node.right != null) { stack.push(node.right); } } Collections.reverse(list); for (Integer i : list) { System.out.print(i + " "); } } /** * 迭代方式的中序遍历 * @param root */ public static void inOrderIter(TreeNode root){ if(root == null){ return; } Stack<TreeNode> stack = new Stack<>(); while (!stack.isEmpty() || root != null){ if(root != null){ stack.push(root); root = root.left; } else { root = stack.pop(); System.out.print(root.val + " "); root = root.right; } } } /** * 层序遍历 * @param root */ public static void levelOrder(TreeNode root){ Queue<TreeNode> queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()){ Queue<TreeNode> next = new LinkedList<>(); for (TreeNode node : queue) { if(node != null){ System.out.print(node.val + " "); if(root.left != null){ next.add(node.left); } if(root.right != null){ next.add(node.right); } }else{ return; } } queue = next; } }}