二叉树的前中后序遍历(递归)
二叉树结点
public calss TreeNode(){
int data;
TreeNode left;
TreeNode right;
public TreeNode(data){
this.data = data;
}
public calss XX{
List list = new ArrayList<>();
ppublic void dfs(TreeNode node){
if(node == null) return;
list.add(node.data);
dfs(node.left);
dfs(node.right);
}
}
public calss ZX{
List list = new ArrayList<>();
ppublic void dfs(TreeNode node){
if(node == null) return;
dfs(node.left);
list.add(node.data);
dfs(node.right);
}
}
public calss HX{
List list = new ArrayList<>();
ppublic void dfs(TreeNode node){
if(node == null) return;
dfs(node.left);
dfs(node.right);
list.add(node.data);
}
}
package cn.kales.algorithm.test.test01;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Tree {
public void cengxu(Node root){
if (root == null)
System.out.println("树为空");
List<Node> list = new ArrayList();
list.add(root);
List<Integer> visit = new ArrayList();
while(!list.isEmpty()){
Node temp = list.remove(0);
if (temp.left != null) list.add(temp.left);
if (temp.left != null) list.add(temp.right);
visit.add(temp.data);
}
}
public void xianxu_1(Node root,List<Integer> visit){
if (root == null) return;
visit.add(root.data);
xianxu_1(root.left,visit);
xianxu_1(root.right,visit);
}
public void xianxu_2(Node root,List<Integer> visit){
if(root == null) return;
Stack<Node> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
Node temp = stack.pop();
visit.add(temp.data);
if(temp.right != null)
stack.push(temp.right);
if (temp.left != null)
stack.push(temp.left);
}
}
public void zhongxu_1(Node root,List<Integer> visit){
if (root == null) return;
zhongxu_1(root.left,visit);
visit.add(root.data);
zhongxu_1(root.right,visit);
}
public void zhongxu_2(Node root,List<Integer> visit){
if (root == null) return;
Stack<Node> stack = new Stack<>();
Node cur = root;
while(cur != null || !stack.isEmpty()){
while(cur != null){
stack.push(cur);
cur = cur.left;
}
Node temp = stack.pop();
visit.add(temp.data);
cur = temp.right;
}
}
public void houxu_1(Node root,List<Integer> visit){
if (root == null) return;
houxu_1(root.left, visit);
houxu_1(root.left, visit);
visit.add(root.data);
}
public void houxu_2(Node root,List<Integer> visit){
Stack<Node> stack = new Stack<>();
Node cur = root;
Node pre = null;
while (cur != null || !stack.isEmpty()){
while(cur != null){
stack.push(cur);
cur = cur.left;
}
Node temp = stack.peek();
if (temp.right == null || temp.right == pre){
stack.pop();
visit.add(temp.data);
pre = temp;
cur = null;
}else{
cur = temp.right;
}
}
}
public void houxu_3(Node root,List<Integer> visit){
if (root == null) return;
Stack<Node> stack1 = new Stack<>();
Stack<Node> stack2 = new Stack<>();
stack1.push(root);
while (!stack1.isEmpty()){
Node temp = stack1.pop();
stack2.push(temp);
if (temp.left != null) stack1.push(temp.left);
if (temp.right != null) stack1.push(temp.right);
}
while(!stack2.isEmpty()){
visit.add(stack1.pop().data);
}
}
}
class Node{
int data;
Node left;
Node right;
public Node (int data){
this.data = data;
}
}