import java.util.ArrayList;
import java.util.Collections;
import java.util.Stack;
/**
* 二叉树的前、中、后三种遍历方式递归实现和非递归实现
*/
public class PrintTree {
//前序遍历递归
public static ArrayList<Integer> preRecursive(ArrayList<Integer> result,TreeNode root){
if(root!=null){
result.add(root.val);
if(root.left!=null){
preRecursive(result,root.left);
}
if (root.right!=null){
preRecursive(result,root.right);
}
}
return result;
}
//中序遍历递归
public static ArrayList<Integer> inRecursive(ArrayList<Integer> result,TreeNode root){
if(root!=null){
if(root.left!=null){
inRecursive(result,root.left);
}
result.add(root.val);
if (root.right!=null){
inRecursive(result,root.right);
}
}
return result;
}
//后序遍历递归
public static ArrayList<Integer> sufRecursive(ArrayList<Integer> result,TreeNode root){
if(root!=null){
if(root.left!=null){
sufRecursive(result,root.left);
}
if (root.right!=null){
sufRecursive(result,root.right);
}
result.add(root.val);
}
return result;
}
/**
* 前序非递归实现
* 核心思路:(栈实现)
* 根节点入栈,top节点出栈,打印出栈节点,出栈节点存在子节点,子节点从左至右依次入栈,
* 循环上一步直至栈空。
* @param root
* @return
*/
public static ArrayList<Integer> preNotRecursive(TreeNode root){
ArrayList<Integer> result = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if(root!=null){
stack.push(root);
}else{
return null;
}
TreeNode tempNode = null;
while (!stack.isEmpty()){
tempNode = stack.pop();
result.add(tempNode.val);
if(tempNode.right!=null){
stack.push(tempNode.right);
}
if(tempNode.left!=null){
stack.push(tempNode.left);
}
}
return result;
}
/**
* 中序非递归实现
* 核心思路:(栈实现)
* 根节点开始找到所有左孩子,依次入栈
* 栈顶弹出最后一个左孩子,打印弹出节点
* 检查弹出节点是否存在右子树,存在压入栈中,
* 当前节点作为根节点循环以上3步,直到栈空
* @param root
* @return
*/
public static ArrayList<Integer> inNotRecursive(TreeNode root){
ArrayList<Integer> result = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if(root==null){
return null;
}
TreeNode tempNode = null;
while (root!=null){
stack.push(root);
tempNode = root;
while (tempNode.left!=null){
stack.push(tempNode.left);
tempNode = tempNode.left;
}
TreeNode top = stack.pop();
result.add(top.val);
root = top.right;
while (root ==null&&!stack.isEmpty()){
top = stack.pop();
result.add(top.val);
root = top.right;
}
}
return result;
}
/**
* 后序非递归实现
* 核心思路:
* 前序遍历左右相反,结果逆序
* @param root
* @return
*/
public static ArrayList<Integer> sufNotRecursive(TreeNode root){
ArrayList<Integer> result = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if(root!=null){
stack.push(root);
}else{
return null;
}
TreeNode tempNode = null;
while (!stack.isEmpty()){
tempNode = stack.pop();
result.add(tempNode.val);
if(tempNode.left!=null){
stack.push(tempNode.left);
}
if(tempNode.right!=null){
stack.push(tempNode.right);
}
}
Collections.reverse(result);
return result;
}
public static void main(String[] args) {
int [] pre={1,2,4,7,3,5,6,8};
int [] in = {4,7,2,1,5,3,8,6};
TreeNode root = ReConstructBinaryTree.reConstructBinaryTree(pre,in);
ArrayList<Integer> result1 = new ArrayList<>();
preRecursive(result1,root);
//[1,2,4,7,3,5,6,8]
System.out.println(result1.toString());
result1 =preNotRecursive(root);
//[1,2,4,7,3,5,6,8]
System.out.println(result1.toString());
ArrayList<Integer> result2 = new ArrayList<>();
inRecursive(result2,root);
// [4, 7, 2, 1, 5, 3, 8, 6]
System.out.println(result2.toString());
result2 = inNotRecursive(root);
// [4, 7, 2, 1, 5, 3, 8, 6]
System.out.println(result2.toString());
ArrayList<Integer> result3 = new ArrayList<>();
sufRecursive(result3,root);
//[7, 4, 2, 5, 8, 6, 3, 1]
System.out.println(result3.toString());
result3 = sufNotRecursive(root);
//[7, 4, 2, 5, 8, 6, 3, 1]
System.out.println(result3.toString());
}
}
二叉树遍历递归和非递归实现
最新推荐文章于 2024-09-24 21:00:19 发布