145. 二叉树的后序遍历 递归 迭代 Morris

  1. 二叉树的后序遍历
    给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历 。

示例 1:

输入:root = [1,null,2,3]
输出:[3,2,1]
示例 2:

输入:root = []
输出:[]
示例 3:

输入:root = [1]
输出:[1]

提示:

树中节点的数目在范围 [0, 100] 内
-100 <= Node.val <= 100

进阶:递归算法很简单,你可以通过迭代算法完成吗?

递归
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ans=new ArrayList<Integer>();
        if(root==null)return ans;
        ans.addAll(postorderTraversal(root.left));
        ans.addAll(postorderTraversal(root.right));
        ans.add(root.val);
        return ans;
    }
}
迭代
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<Integer>();
        if (root == null) return ans;
        Deque<TreeNode> stk = new LinkedList<TreeNode>();
        TreeNode pre = null;
        while (root != null || !stk.isEmpty()) {
            while (root != null) {
                stk.push(root);
                root = root.left;
            }
            root = stk.pop();
            if (root.right == null || root.right == pre) {
                //第二次访问,或者是右子树为空的情况,记录答案,记录pre,置root为空
                ans.add(root.val);
                pre = root;
                root = null;
            } else {
                //第一次访问节点,将自己压回栈,然后进入右子树
                stk.push(root);
                root = root.right;
            }
        }
        return ans;
    }
}

Morris
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        TreeNode rt=root;
        List<Integer> ans = new ArrayList<Integer>();
        if (root == null) return ans;

        while (root != null) {
            if (root.left != null) {
                TreeNode pre = root.left;
                while (pre.right != null && pre.right != root) pre=pre.right;
                if (pre.right == null) {
                    pre.right = root;
                    root = root.left;
                } else {
                    pre.right = null;//这条不能省略,因为后面访问斜的路径还要以它来判断终点
                    addPath(ans, root.left);
                    root = root.right;
                }
            }
            else {
                root = root.right;
            }
        }
        addPath(ans, rt);
        return ans;
    }

    访问一斜行,类似于adidas
    public void addPath(List<Integer> ans, TreeNode node) {
        int cnt = 0;
        while (node != null) {
            ++cnt;
            ans.add(node.val);
            node = node.right;
        }
        int l = ans.size() - cnt, r = ans.size() - 1;
        while (l < r) {
            int t = ans.get(l);
            ans.set(l, ans.get(r));
            ans.set(r, t);
            l++;
            r--;
        }
    }

    // public void addPath(List ans, TreeNode node) {
    //     if(node==null)return ;
    //     addPath(ans,node.right);
    //     ans.add(node.val);
    // }

}

你可能感兴趣的:(leetcode,深度优先,leetcode,数据结构)