二叉树的节点声明如下:
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x){
val = x;
left = null;
right = null;
}
}
1. 先序遍历:(前为递归实现,后为非递归实现)
public class shu {
//树递归的先序遍历 (根 左 右)
public static void main(String[] args) {
TreeNode tn = new TreeNode(4);
TreeNode tn1 = new TreeNode(5);
TreeNode tn2 = new TreeNode(6);
TreeNode tn3 = new TreeNode(7);
TreeNode tn4 = new TreeNode(8);
TreeNode tn5 = new TreeNode(9);
TreeNode tn6 = new TreeNode(10);
tn.left = tn1;
tn.right = tn2;
tn1.left=tn3;
tn1.right=tn4;
tn2.left=tn5;
tn2.right=tn6;
shu s = new shu();
s.DPreOrderTraversal(tn);
}
public void DPreOrderTraversal(TreeNode root)
{
if(root!=null){
System.out.println(root.val); //对节点做些访问比如打印
DPreOrderTraversal(root.left); //访问左儿子
DPreOrderTraversal(root.right); //访问右儿子
}
}
}
public class shu {
//树非递归的先序遍历 和 中序比较相似。
//树非递归的先序遍历 (根 左 右)
public static void main(String[] args) {
TreeNode tn = new TreeNode(4);
TreeNode tn1 = new TreeNode(5);
TreeNode tn2 = new TreeNode(6);
TreeNode tn3 = new TreeNode(7);
TreeNode tn4 = new TreeNode(8);
TreeNode tn5 = new TreeNode(9);
TreeNode tn6 = new TreeNode(10);
tn.left = tn1;
tn.right = tn2;
tn1.left=tn3;
tn1.right=tn4;
tn2.left=tn5;
tn2.right=tn6;
shu s = new shu();
s.PreOrderTraversal(tn);
}
public void PreOrderTraversal(TreeNode root)
{
Stack<TreeNode> stack = new Stack<>(); //创建并初始化堆栈stack
while(!stack.empty() || root!=null)
{
while(root!=null)
{
System.out.println(root.val);//一直向左并将沿途节点访问(打印)后压入堆栈
stack.push(root);
root = root.left;
}
root = stack.pop();
root = root.right;
}
}
}
2. 中序遍历:(前为递归实现,后为非递归实现)
public class shu {
//树递归的中序遍历 (左 根 右)
public static void main(String[] args) {
TreeNode tn = new TreeNode(4);
TreeNode tn1 = new TreeNode(5);
TreeNode tn2 = new TreeNode(6);
TreeNode tn3 = new TreeNode(7);
TreeNode tn4 = new TreeNode(8);
TreeNode tn5 = new TreeNode(9);
TreeNode tn6 = new TreeNode(10);
tn.left = tn1;
tn.right = tn2;
tn1.left=tn3;
tn1.right=tn4;
tn2.left=tn5;
tn2.right=tn6;
shu s = new shu();
s.DMidOrderTraversal(tn);
}
public void DMidOrderTraversal(TreeNode root)
{
if(root!=null){
DMidOrderTraversal(root.left); //访问左儿子
System.out.println(root.val); //对节点做些访问比如打印
DMidOrderTraversal(root.right); //访问右儿子
}
}
}
public class shu {
//树非递归的先序遍历 和 中序比较相似。
//树非递归的中序遍历 (左 根 右)
public static void main(String[] args) {
TreeNode tn = new TreeNode(4);
TreeNode tn1 = new TreeNode(5);
TreeNode tn2 = new TreeNode(6);
TreeNode tn3 = new TreeNode(7);
TreeNode tn4 = new TreeNode(8);
TreeNode tn5 = new TreeNode(9);
TreeNode tn6 = new TreeNode(10);
tn.left = tn1;
tn.right = tn2;
tn1.left=tn3;
tn1.right=tn4;
tn2.left=tn5;
tn2.right=tn6;
shu s = new shu();
s.MidOrderTraversal(tn);
}
public void MidOrderTraversal(TreeNode root)
{
Stack<TreeNode> stack = new Stack<>(); //创建并初始化堆栈stack
while(!stack.empty() || root!=null)
{
while(root!=null)
{
stack.push(root);
root = root.left;
}
root = stack.pop();
System.out.println(root.val);
root = root.right;
}
}
}
3. 后序遍历:(前为递归实现,后为非递归实现)
public class shu {
//树递归的后序遍历 (左 右 根 )
public static void main(String[] args) {
TreeNode tn = new TreeNode(4);
TreeNode tn1 = new TreeNode(5);
TreeNode tn2 = new TreeNode(6);
TreeNode tn3 = new TreeNode(7);
TreeNode tn4 = new TreeNode(8);
TreeNode tn5 = new TreeNode(9);
TreeNode tn6 = new TreeNode(10);
tn.left = tn1;
tn.right = tn2;
tn1.left=tn3;
tn1.right=tn4;
tn2.left=tn5;
tn2.right=tn6;
shu s = new shu();
s.DPostOrderTraversal(tn);
}
public void DPostOrderTraversal(TreeNode root)
{
if(root!=null){
DPostOrderTraversal(root.left); //访问左儿子
DPostOrderTraversal(root.right); //访问右儿子
System.out.println(root.val); //访问根
}
}
}
public class shu {
//树非递归的后序遍历 (左 右 根)
public static void main(String[] args) {
TreeNode tn = new TreeNode(4);
TreeNode tn1 = new TreeNode(5);
TreeNode tn2 = new TreeNode(6);
TreeNode tn3 = new TreeNode(7);
TreeNode tn4 = new TreeNode(8);
TreeNode tn5 = new TreeNode(9);
TreeNode tn6 = new TreeNode(10);
tn.left = tn1;
tn.right = tn2;
tn1.left=tn3;
tn1.right=tn4;
tn2.left=tn5;
tn2.right=tn6;
List list = new ArrayList();
shu s= new shu();
list = s.postorderTraversal(tn);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
public List<Integer> postorderTraversal(TreeNode root){
List res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while(!stack.isEmpty()||root!=null){
while(root!=null){
res.add(0,root.val);
stack.push(root);
root = root.right;
}
root = stack.pop();
root = root.left;
}
return res;
}
}