二叉树的遍历(递归+非递归)

时间:2021-01-07 11:25:24

二叉树的定义:

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

1、先序遍历 preorder traversal

递归实现:

public class Solution {
    private ArrayList<Integer> arr = new ArrayList<Integer>();
    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        if(root == null)    return arr;
        arr.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return arr;
    }
}

非递归实现:

public class Solution {
    private ArrayList<Integer> arr = new ArrayList<Integer>();
    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        if(root == null)    return arr;
        // arr.add(root.val);
        // preorderTraversal(root.left);
        // preorderTraversal(root.right);
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);
        while(!stack.empty()) {
        	TreeNode p = stack.peek();
        	while(p != null) {
        		arr.add(p.val);
        		stack.push(p.left);
        		p = stack.peek();
        	}
        	stack.pop();
        	if(!stack.empty()) {
        		p = stack.pop();
        		stack.push(p.right);
        	}
        }
        return arr;
    }
}

2、中序遍历 inorder traversal

递归实现:

public class Solution {
    private ArrayList<Integer> arr = new ArrayList<Integer>();
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        if(root == null)    return arr;
        if(root.left != null)
            inorderTraversal(root.left);
        arr.add(root.val);
        if(root.right != null)
            inorderTraversal(root.right);
        return arr;
    }
}

非递归实现:

public class Solution {
    private ArrayList<Integer> arr = new ArrayList<Integer>();
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        if(root == null)    return arr;
        // if(root.left != null)
        //     inorderTraversal(root.left);
        // arr.add(root.val);
        // if(root.right != null)
        //     inorderTraversal(root.right);
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);
        while(!stack.empty()) {
            TreeNode p = stack.peek();
            while(p != null) {
                stack.push(p.left);
                p = stack.peek();
            }
            stack.pop();    // pop the empty point
            if(!stack.empty()) {
                p = stack.pop();
                arr.add(p.val);
                stack.push(p.right);
            }
        }
        return arr;
    }
}

3、后序遍历 postorder traversal

递归实现:

public class Solution {
    private ArrayList<Integer> arr = new ArrayList<Integer>();
    public ArrayList<Integer> postorderTraversal(TreeNode root) {
        if(root == null)    return arr;
        if(root.left != null)   postorderTraversal(root.left);
        if(root.right != null)  postorderTraversal(root.right);
        arr.add(root.val);
        return arr;
    }
}

非递归实现:

后续遍历的非递归实现较先序和中序有大不同,因为此时除叶子节点外的每个节点都会被访问到两次。本方法采用的是:如果左右孩子都为空,则访问该节点;或者当前节点的左右孩子都已经被访问过,则访问该节点,并弹出栈。入栈时,若有右孩子,则右孩子先进栈,然后左孩子进栈,这样是为了保证出栈时先访问左孩子。

public class Solution {
    private ArrayList<Integer> arr = new ArrayList<Integer>();
    public ArrayList<Integer> postorderTraversal(TreeNode root) {
        if(root == null)    return arr;
        // if(root.left != null)   postorderTraversal(root.left);
        // if(root.right != null)  postorderTraversal(root.right);
        // arr.add(root.val);
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);
        TreeNode pre = null;
        while(!stack.empty()) {
            TreeNode p = stack.peek();
            // left and right child are null, visit present node
            // previous visited node is present node's left or right child, visit present node
            if((p.left == null && p.right == null) || 
                (pre != null && (pre == p.left || pre == p.right))) {
                arr.add(p.val);
                stack.pop();
                pre = p;
            }
            else {
                // first push the right child, then push the left child 
                // to ensure first visit the left child
                if(p.right != null)  stack.push(p.right);
                if(p.left != null)  stack.push(p.left);
            }
        }
        return arr;
    }
}