复习一下二叉树的前中后序遍历
就我所知道的,有递归和非递归两种方法。
//递归版前序遍历 public static void TreePreOrder(TreeNode node){ System.out.println(node.val); TreeNode leftTree = node.left; if (leftTree != null){ TreePreOrder(leftTree); } TreeNode rightTree = node.right; if(rightTree != null){ TreePreOrder(rightTree); } } //递归版中序遍历 public static void TreeInOrder(TreeNode node){ if(node == null) return; TreeInOrder(node.left); System.out.println(node.val); TreeInOrder(node.right); } //递归版后序遍历 public static void TreePostOrder(TreeNode node){ if(node == null) return; TreePostOrder(node.left); TreePostOrder(node.right); System.out.println(node.val); }