MENU

LeetCode144.二叉树的前序遍历&94.二叉树的中序遍历&145.二叉树的后序遍历

August 8, 2018 • Read: 3221 • LeetCode阅读设置

题目链接:LeetCode144

递归代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    List<Integer> res = new ArrayList<Integer>();
    public List<Integer> preorderTraversal(TreeNode root) {
        if(root == null)
            return res;
        res.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return res;
    }
}
非递归代码
class Solution {
    List<Integer> res = new ArrayList<Integer>();
    public List<Integer> preorderTraversal(TreeNode root) {
        res.clear();
        if(root == null)
            return res;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);
        while(!stack.isEmpty()) {
            TreeNode node = stack.pop();
            res.add(node.val);
            if(node.right != null)
                stack.add(node.right);
            if(node.left != null)
                stack.add(node.left);
        }
        return res;
    }
    
}

题目链接:LeetCode94

递归代码
class Solution {
    List<Integer> res = new LinkedList<Integer>();
    public List<Integer> inorderTraversal(TreeNode root) {
        if(root == null)
            return res;
        inorderTraversal(root.left);
        res.add(root.val);
        inorderTraversal(root.right);
        return res;
    }
}
非递归代码
class Solution {
    List<Integer> res = new LinkedList<Integer>();
    public List<Integer> inorderTraversal(TreeNode root) {
        res.clear();
        if(root == null)
            return res;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        while(!stack.isEmpty() || root != null) {
            if(root != null) {
                stack.push(root);
                root = root.left;
            } else {
                root = stack.pop();
                res.add(root.val);
                root = root.right;
            }
        }
        return res;
    }
}

题目链接:LeetCode145

递归代码
class Solution {
    List<Integer> res = new ArrayList<Integer>();
    public List<Integer> postorderTraversal(TreeNode root) {
        res.clear();
        if(root == null)
            return res;
        postorderTraversal(root.left);
        postorderTraversal(root.right);
        res.add(root.val);
        return res;
    }
}
思路

后序遍历的非递归代码得说一下,首先后序遍历得顺序是左右中,我们知道先序遍历的压栈顺序是先压右再压左,这样出来的顺序就是中左右,如果把先序遍历的压栈顺序稍微变一下,变成先压左再压右,这样出栈的顺序就是中右左,和后序遍历的顺序正好是相反的,把得到的中右左放到一个辅助栈里依次打印出来就变成左右中了

非递归代码
class Solution {
    List<Integer> res = new ArrayList<Integer>();
    public List<Integer> postorderTraversal(TreeNode root) {
        res.clear();
        if(root == null)
            return res;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        Stack<TreeNode> help = new Stack<TreeNode>();
        stack.push(root);
        while(!stack.isEmpty()) {
            TreeNode node = stack.pop();
            help.push(node);
            if(node.left != null)
                stack.add(node.left);
            if(node.right != null)
                stack.add(node.right);
        }
        while(!help.isEmpty())
            res.add(help.pop().val);
        return res;
    }
    
}
Last Modified: May 12, 2021
Archives Tip
QR Code for this page
Tipping QR Code