二叉树的节点定义如下:
//节点
class TreeNode{
int val;
TreeNode left,right;
TreeNode(int val){
this.val=val;
}
}
二叉树的前序遍历
二叉树的前序遍历顺序为:根左右。如下图所示,前序遍历顺序为:1245367。
递归
递归代码:
public static void preOrder(TreeNode root){
if(root==null){
return;
}
System.out.print(root.val+" ");
preOrder(root.left);
preOrder(root.right);
}
非递归
非递归实现主要用到了Stack容器。
public static void preOrder2(TreeNode root){
if(root==null){
return;
}
Stack<TreeNode> stack=new Stack<>();
while(!stack.isEmpty() || root!=null){
if(root!=null){
//打印根的值
System.out.print(root.val+" ");
//压入当前节点
stack.push(root);
//往左子树方向走
root=root.left;
}else{
//弹出节点
TreeNode node=stack.pop();
root=node.right;
}
}
}
二叉树的中序遍历
二叉树的中序遍历顺序为:左根右。如下图所示,中序遍历顺序为:4251637 。
递归
public static void midOrder(TreeNode root){
if(root==null){
return;
}
midOrder(root.left);
System.out.print(root.val+" ");
midOrder(root.right);
}
非递归
非递归实现主要用到了Stack容器。
public static void midOrder2(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{
TreeNode node=stack.pop();
System.out.print(node.val+" ");
root=node.right;
}
}
}
二叉树的后序遍历
二叉树的后序遍历顺序为:左右根。如下图所示,中序遍历顺序为:4526731 。
递归
public static void postOrder(TreeNode root){
if(root==null){
return;
}
postOrder(root.left);
postOrder(root.right);
System.out.print(root.val+" ");
}
非递归
非递归实现主要用到了Stack容器.
public static void postOrder2(TreeNode root){
if(root==null){
return;
}
Stack<TreeNode> stack=new Stack<>();
//记录前一个输出过的节点
TreeNode preNode=null;
while(!stack.isEmpty() || root!=null){
if(root!=null){
//不断往左子节点走
stack.push(root);
root=root.left;
}else{
TreeNode node=stack.peek();
//如果当前节点有右子节点,且右子节点没有被访问过
if(node.right!=null && node.right!=preNode){
root=node.right;
}else{
//当前节点没有右子节点或者右子节点被访问过,则弹出。
stack.pop();
System.out.print(node.val+" ");
//标记访问过的节点
preNode=node;
root=null;
}
}
}
}
测试
代码:
class TreeNode{
int val;
TreeNode left,right;
TreeNode(int val){
this.val=val;
}
}
//建树
class Tree{
}
public class Test{
public static void main(String[] args){
TreeNode root=createTree();
System.out.println("前序遍历(递归):");
preOrder(root);
System.out.println();
System.out.println("前序遍历(非递归):");
preOrder2(root);
System.out.println();
System.out.println("中序遍历:");
midOrder(root);
System.out.println();
System.out.println("中序遍历(非递归):");
midOrder2(root);
System.out.println();
System.out.println("后序遍历:");
postOrder(root);
System.out.println();
System.out.println("后序遍历(非递归):");
postOrder2(root);
}
public static TreeNode createTree(){
TreeNode root=new TreeNode(1);
TreeNode node1=new TreeNode(2);
TreeNode node2=new TreeNode(3);
root.left=node1;
root.right=node2;
node1.left=new TreeNode(4);
node1.right=new TreeNode(5);
node2.left=new TreeNode(6);
node2.right=new TreeNode(7);
return root;
}
//前序遍历
public static void preOrder(TreeNode root){
//....
}
//前序遍历 非递归
public static void preOrder2(TreeNode root){
//...
}
//中序遍历
public static void midOrder(TreeNode root){
//...
}
//中序遍历 非递归
public static void midOrder2(TreeNode root){
//...
}
//后续遍历
public static void postOrder(TreeNode root){
//...
}
//后续遍历 非递归
public static void postOrder2(TreeNode root){
//...
}
}
结果如下: