非递归遍历二叉树Java版的实现代码(没写层次遍历)

直接上代码呵呵,里面有注解

 

package www.com.leetcode.specificProblem;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;


/**
 * 总结了三种非递归遍历二叉树
 * @author 85060
 *
 */
public class TraverseBinaryTree {

    
    //用来装遍历序列的一个list,展示的时候用
    private List<TreeNode> helpList = new ArrayList<>();
    
    public static void main(String[] args) {
        TreeNode root = createATree();
        TraverseBinaryTree traverseBinaryTree = new TraverseBinaryTree();
        
        
        //traverseBinaryTree.frontTraverseRecursion(root);
        //traverseBinaryTree.midTraverseRecursion(root);
        traverseBinaryTree.backTraverseRecursion(root);
        String rightAnswer = traverseBinaryTree.helpList.toString();
        System.out.println("right: " + rightAnswer);
        
        traverseBinaryTree.helpList.clear();
        
        //traverseBinaryTree.frontTravese(root);
        //traverseBinaryTree.midTraverse(root);
        traverseBinaryTree.backTraverse(root);
        String myAnswer = traverseBinaryTree.helpList.toString();
        System.out.println("my ans: " + myAnswer);
        System.out.println("bingo? " + rightAnswer.equals(myAnswer));
        
        
    }
    
    
    /**
     * 递归版的前序遍历二叉树
     * @param node
     */
    private void frontTraverseRecursion(TreeNode node) {
        if(node == null)return;
        helpList.add(node);
        frontTraverseRecursion(node.left);
        frontTraverseRecursion(node.right);
    }
    
    
    /**
     * 递归式的中序递归二叉树
     * @param node
     */
    private void midTraverseRecursion(TreeNode node) {
        if(node == null)return;
        midTraverseRecursion(node.left);
        helpList.add(node);
        midTraverseRecursion(node.right);
    }
    
    
    /**
     * 递归式的后序遍历二叉树
     * @param node
     */
    private void backTraverseRecursion(TreeNode node) {
        if(node == null)return;
        backTraverseRecursion(node.left);
        backTraverseRecursion(node.right);
        helpList.add(node);
    }
    
    
    /**
     * 非递归的前序遍历二叉树
     * 用一个node,一直往左遍历,一边遍历一边入栈,当走到左边的尽头后,就出栈,然后去这个出栈元素的右子树,继续循环
     * @param root
     */
    private void frontTravese(TreeNode root) {
        if(root == null)return;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode node = root;
        
        while(node != null || !stack.isEmpty()) {
            if(node != null) {
                helpList.add(node);//visit
                stack.push(node);
                node = node.left;
            } else {
                //左子树没了,进入栈中然后去右子树咯
                node = stack.pop().right;
            }
        }
    }
    
    /**
     * 非递归式的中序递归二叉树
     * @param root
     */
    private void midTraverse(TreeNode root) {
        if(root == null)return ;
        
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode node = root;
        
        while(node != null || !stack.isEmpty()) {
            
            while(node != null) {//一直往左走到尽头
                stack.push(node);
                node = node.left;
            }
            if(!stack.isEmpty()) {
                node = stack.pop();
                helpList.add(node);//visit
                node = node.right;//进入右子树
            }
        }
        
    }
    
    
    /**
     * 非递归的后序遍历二叉树
     * 后序遍历的关键就是要判断上一个结点的右孩子访问了没,所以要存上次访问的结点
     * @param root
     */
    private void backTraverse(TreeNode root) {
        if(root == null)return;
        
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode node = root;
        TreeNode lastVisit = null;//记录刚刚访问过的那个结点,用来区分stack中pop出来的结点的右子树访问过没
        
        
        while(node != null || !stack.isEmpty()) {
            while(node != null) {//一直走到树的最左边,stack是最左边的那个叶子结点
                stack.push(node);
                node = node.left;
            }
            
            while(!stack.isEmpty()) {
                node = stack.peek();
                if(node.right == null || node.right == lastVisit) {//没有右子树,右子树访问过了,都可以访问这个结点了
                    helpList.add(node);//visit
                    //System.out.println(node);
                    lastVisit = node;
                    stack.pop();
                    if(stack.isEmpty())return;//这个很重要,不然会无限循环
                } else {//右子树不为空,而且没访问过
                    node = node.right;
                    break;//出去要走到这个右子树的最左边噢
                }
            }
        }
        
    }
    
    
    /**
     * 构造一颗测试用的二叉树数据
     * @return
     */
    private static TreeNode createATree() {
        
        TreeNode root = new TreeNode(4);
        root.left = new TreeNode(3);
        root.right = new TreeNode(6);
        root.left.left = new TreeNode(11);
        root.left.right = new TreeNode(12);
        root.left.right.left = new TreeNode(45);
        root.right.left = new TreeNode(13);
        root.right.right = new TreeNode(14);
        root.right.right.right = new TreeNode(32);
        
        return root;
        
    }
}


/**
 * 二叉树结点类
 * @author 85060
 *
 */
class TreeNode {
    int data;
    TreeNode left;
    TreeNode right;
    
    public TreeNode(int data) {
        this.data = data;
    }
    
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return data + "";
    }
}

 

posted @ 2019-04-21 14:10  汪神  阅读(547)  评论(0编辑  收藏  举报