zoukankan      html  css  js  c++  java
  • LeetCode145. 二叉树的后序遍历

    class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
            /**
             * 方法1:递归
             */
            /*
            List<Integer> res = new ArrayList<>();
            dfs(root, res);
            return res;
            */
            /**
             *  方法2:非递归
             */
            List<Integer> res = new ArrayList<>();
            Stack<TreeNode> stack = new Stack<>();
            TreeNode pre = null; // 用于记录前一次访问的节点
            while (!stack.isEmpty() || root != null) {
                while (root != null) {
                    stack.push(root);
                    root = root.left;
                }
                root = stack.peek();
                // 如果右节点为空 或右节点访问过
                if (root.right == null || root.right == pre) {
                    res.add(root.val); // 此时可以访问根结点
                    stack.pop();
                    pre = root;
                    root = null;  // 此时下一轮循环不要将左子树压栈,直接判断栈顶元素
                }else {
                    root = root.right; // 先不出栈 把它右节点入栈
                }
            }
            return res;
            /**
             * 方法3:非递归的另一种方法
             *      修改前序遍历代码中,节点写入结果链表的代码:将插入队尾修改为插入队首
             *      修改前序遍历代码中,每次先查看左节点再查看右节点的逻辑:变为先查看右节点再查看左节点
             */
            /*
            List<Integer> res = new ArrayList<>();
            Stack<TreeNode> stack = new Stack<>();
            while (!stack.isEmpty() || root != null) {
                while (root != null) {
                    res.add(0,root.val);  // 取根节点的值,插入list最后边
                    stack.push(root);
                    root = root.right; // 遍历右子树
                }
                root = stack.pop();
                root = root.left; // 遍历左子树
            }
            return res;
            */
        }
        private void dfs(TreeNode root, List<Integer> res) {
            if (root == null) return;
            dfs(root.left, res); //
            dfs(root.right, res);//
            res.add(root.val);//
        }
    }
  • 相关阅读:
    用GitHub Pages搭建博客(三)
    Beta阶段项目总结
    最终团队绩效评估
    Alpha阶段项目总结
    项目发布
    Alpha版总结会议
    第二次冲刺周期站立会议(10)
    第二次冲刺周期站立会议(9)
    第二次冲刺周期站立会议(8)
    第二次冲刺周期站立会议(7)
  • 原文地址:https://www.cnblogs.com/HuangYJ/p/14156845.html
Copyright © 2011-2022 走看看