二叉树的先序、中序、后序、层次遍历递归与非递归方法
/**
* @Description:先序遍历二叉树(根左右)
*/
//递归
public ArrayList<Integer> preOrderRe(TreeNode root,ArrayList<Integer> list){
if(root == null){
return list;
}
list.add(root.val);
if(root.left!=null){
preOrderRe(root.left,list);
}
if(root.right!=null){
preOrderRe(root.right,list);
}
return list;
}
//非递归
public ArrayList<Integer> preOrder(TreeNode root,ArrayList<Integer> list){
if(root == null){
return null;
}
Stack<TreeNode> stack = new Stack<>();
while(root!=null || !stack.isEmpty()){
while(root!=null){
list.add(root.val);
stack.push(root);
root = root.left;
}
if(!stack.isEmpty()){
root = stack.pop();
root = root.right;
}
}
return list;
}
/**
* @Description:中序遍历二叉树(左根右)
*/
//递归
public ArrayList<Integer> midOrderRe(TreeNode root,ArrayList<Integer> list){
if(root == null){
return list;
}
if(root.left!=null){
midOrderRe(root.left,list);
}
list.add(root.val);
if(root.right!=null){
midOrderRe(root.right,list);
}
return list;
}
//非递归
public ArrayList<Integer> midOrder(TreeNode root,ArrayList<Integer> list){
if(root == null){
return list;
}
Stack<TreeNode> stack = new Stack<>();
while(!stack.isEmpty() || root!=null){
while(root!=null){
stack.push(root);
root = root.left;
}
if(!stack.isEmpty()){
root = stack.pop();
list.add(root.val);
root = root.right;
}
}
return list;
}
/**
* @Description:后序遍历二叉树(左右根)
*/
//递归
public ArrayList<Integer> postOrderRe(TreeNode root,ArrayList<Integer> list){
if(root == null){
return list;
}
if(root.left!=null){
postOrderRe(root.left,list);
}
if(root.right!=null){
postOrderRe(root.right,list);
}
list.add(root.val);
return list;
}
//非递归 后序遍历非递归方法需要一个中间栈来实现
public ArrayList<Integer> postOrder(TreeNode root,ArrayList<Integer> list){
if(root == null){
return list;
}
Stack<TreeNode> stack = new Stack<>();
Stack<TreeNode> midStack = new Stack<>();
while(root != null || !stack.isEmpty()){
if(root!=null){
stack.push(root);
midStack.push(root);
root = root.right;
}else{
root = stack.pop();
root = root.left;
}
}
while(!midStack.isEmpty()){
list.add(midStack.pop().val);
}
return list;
}
/**
* @Description:层次遍历二叉树
*/
public ArrayList<Integer> levelOrder(TreeNode root,ArrayList<Integer> list){
ArrayList<TreeNode> nodeList = new ArrayList<>();
if(root == null){
return list;
}
nodeList.add(root);
while(nodeList.size()>0){
//把最先的弹出
TreeNode node = nodeList.remove(0);
if(node.left!=null){
nodeList.add(node.left);
}
if(node.right!=null){
nodeList.add(node.right);
}
list.add(node.val);
}
return list;
}
//测试
@Test
public void testpreOrderRe(){
TreeNode root = new TreeNode(0);
TreeNode node1 = new TreeNode(1);
TreeNode node2 = new TreeNode(2);
TreeNode node3 = new TreeNode(3);
TreeNode node4 = new TreeNode(4);
TreeNode node5 = new TreeNode(5);
TreeNode node6 = new TreeNode(6);
TreeNode node7 = new TreeNode(7);
root.left = node1;
root.right = node2;
node1.left = node3;
node1.right = node4;
node2.left = node5;
node2.right = node6;
node5.left = node7;
ArrayList<Integer> list = new ArrayList<>();
System.out.println(levelOrder(root,list));
}