二叉树的遍历(java版)


节点的定义:

Class BinaryTreeNode{ 
    BinaryTreeNode leftchild; 
    BinaryTreeNode rightchild; 
    int val; 
    BinaryTreeNode(int newval){ 
		leftchild=null; 
		rightchild=null; 
		val=newval; 
}

##层次遍历
按层对树进行遍历

import java.util.LinkedList;

public class ABADemo {
    static class BinaryTreeNode{
        BinaryTreeNode leftChild;
        BinaryTreeNode rightChild;
        int val;
        public BinaryTreeNode(int val){
            this.val=val;
        }
    }
    public static void main(String[] args){
        BinaryTreeNode a = new BinaryTreeNode(1);
        BinaryTreeNode b = new BinaryTreeNode(2);
        BinaryTreeNode c = new BinaryTreeNode(3);
        BinaryTreeNode d= new BinaryTreeNode(4);
        BinaryTreeNode e = new BinaryTreeNode(5);
        a.leftChild = b;
        b.rightChild=c;
        a.rightChild=d;
        d.leftChild=e;
        BinartTreeLevelOrder(a);
    }

    /**
     * 树的层次遍历
     * @param node
     */
    public static void BinartTreeLevelOrder(BinaryTreeNode node){
        if (node==null){
            return;
        }
        LinkedList<BinaryTreeNode> treeNodeArrayList = new LinkedList<>();
        treeNodeArrayList.add(node);
        while (!treeNodeArrayList.isEmpty())
        {
            node=treeNodeArrayList.pop();
            oprator(node);//对节点进行操作的方法
            if (node.leftChild!=null)
            {
                treeNodeArrayList.add(node.leftChild);
            }
            if (node.rightChild!=null)
            {
                treeNodeArrayList.add(node.rightChild);
            }
        }
    }
    public static void oprator(BinaryTreeNode node){
        System.out.println(node.val);
    }

}

##先序遍历
首先访问根结点然后遍历左子树,最后遍历右子树。在遍历左、右子树时,
仍然先访问根结点,然后遍历左子树,最后遍历右子树,如果二叉树为空则返回。

  1. 递归
public void BinaryTreePreOrder(BinaryTreeNode node){
    	if(node==null){
			return;
		}
		System.out.println(node.val);
		BinaryTreePreOrder(node.leftChild);
		BinaryTreePreOrder(node.rightChild);
	}
  1. 非递归
	public void BinaryTreeNonCyclePreOrder(BinaryTreeNode node){
		Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
		while(node!=null||!stack.empty()){
			while(node!=null){
				System.out.println(node.val);
				stack.push(node);
				node = node.leftChild;
			}
			if(!stack.empty()){
				node = stack.pop();
				node = node.rightChild;
			}
		}
	}

##中序遍历
首先遍历左子树,然后访问根结点,最后遍历右子树。如果二叉树为空则返回。

  1. 递归
public void BinaryTreeInOrder(BinaryTreeNode node){
    	if(node==null){
			return;
		}		
		BinaryTreePreOrder(node.leftChild);
		System.out.println(node.val);
		BinaryTreePreOrder(node.rightChild);
	}
  1. 非递归
public void BinaryTreeNonCycleInOrder(BinaryTreeNode node){
		Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
		while(node!=null || !stack.empty()){
			while(node!=null){
				stack.push(node);
				node = node.leftChild;
			}
			if(!stack.empty()){
				node = stack.pop();
				System.out.println(node.val);
				node = node.rightChild;
			}
		}
	}

##后序遍历
首先遍历左子树,然后遍历右子,最后树访问根结点。如果二叉树为空则返回。

  1. 递归
public void BinaryTreePostOrder(BinaryTreeNode node){
    	if(node==null){
			return;
		}		
		BinaryTreePreOrder(node.leftChild);		
		BinaryTreePreOrder(node.rightChild);
		System.out.println(node.val);
	}
  1. 非递归
/**
     * 二叉树后序遍历(非递归)
     * @param node
     */
    private static void BinaryTreeNonCycPostOrder(BinaryTreeNode node) {
        Stack<BinaryTreeNode> treeNodeStack = new Stack<>();
        BinaryTreeNode newNode = node;
        while (node!=null)
        {
            while (node.leftChild!=null)
            {
                treeNodeStack.push(node);
                node=node.leftChild;
            }
            while (node!=null&&(node.rightChild==null||node.rightChild==newNode))
            {
                oprator(node);
                newNode=node;
                if (treeNodeStack.empty())
                {
                    return;
                }
                node=treeNodeStack.pop();
            }
            treeNodeStack.push(node);
            node=node.rightChild;
        }

    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值