树的前序|中序|后序遍历【使用栈实现】

访问一个节点:

  1. 一直往左走

  2. 到最左节点

  3. 输出

  4. 回到父节点

  5. 去右边

    class Solution {

    复制代码
     public List<Integer> inorderTraversal(TreeNode root) {
    
    
         List<Integer> ans = new ArrayList<>();
    
    
         Stack<TreeNode> stack = new Stack<>();
    
    
         TreeNode cur = root;
    
    
    
         while(cur != null || !stack.isEmpty()){
    
    
             // 1. 一直向左走
             while(cur != null){
    
                 stack.push(cur);
    
                 cur = cur.left;
    
             }
    
    
    
             // 2. 弹出节点访问
             cur = stack.pop();
    
             ans.add(cur.val);
    
    
    
             // 3. 转向右子树
             cur = cur.right;
    
         }
    
    
         return ans;
    
     }

    }

那么栈应该模拟:

  1. 访问节点

  2. 保存右节点

  3. 去左边

  4. 左边结束,再去右边

    class Solution {

    复制代码
     public List<Integer> preorderTraversal(TreeNode root) {
    
    
         List<Integer> ans = new ArrayList<>();
    
    
         if(root == null){
             return ans;
         }
    
    
         Stack<TreeNode> stack = new Stack<>();
    
    
         // 根节点入栈
         stack.push(root);
    
    
    
         while(!stack.isEmpty()){
    
    
             // 弹出节点
             TreeNode node = stack.pop();
    
    
             // 访问根
             ans.add(node.val);
    
    
    
             // 注意顺序
             // 栈后进先出
             // 所以右边先入栈
             if(node.right != null){
    
                 stack.push(node.right);
    
             }
    
    
             // 左边后入栈
             if(node.left != null){
    
                 stack.push(node.left);
    
             }
    
         }
    
    
         return ans;
     }

    }

前序:

根 左 右

如果改一下:

先:

根 右 左

然后反转:

左 右 根

也就是后序。

复制代码
class Solution {

    public List<Integer> postorderTraversal(TreeNode root) {


        List<Integer> ans = new ArrayList<>();


        if(root == null){
            return ans;
        }



        Stack<TreeNode> stack = new Stack<>();


        stack.push(root);



        while(!stack.isEmpty()){


            TreeNode node = stack.pop();


            // 先加入结果
            ans.add(node.val);



            // 左先入
            if(node.left != null){

                stack.push(node.left);

            }


            // 右后入
            if(node.right != null){

                stack.push(node.right);

            }

        }



        // 反转结果
        Collections.reverse(ans);


        return ans;

    }
}
相关推荐
不会就选b1 小时前
算法日常・每日刷题--<贪心>25
数据结构·算法
Cccp.1234 小时前
【leetcode】(七)前缀树和贪心算法
数据结构·leetcode·贪心算法
千里码aicood4 小时前
基于CART算法的图书分类系统设计与实现
数据库·算法·分类
做cv的小昊5 小时前
【World Model】π0.5:a Vision-Language-Action Model with Open-World Generalization
人工智能·算法·机器学习·大模型·多模态·vla·世界模型
لا معنى له5 小时前
World-In-World: World Models in a Closed-Loop World
算法
Logic1017 小时前
C语言/数据结构位运算题解:异或XOR找出多任务下载器中的“独特下载速度“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
Discipline10297 小时前
尼泊尔河流流量:天气驱动的预测与高流量风险
人工智能·算法
XuCoder8 小时前
改一个数,右边全得重算,这题怎么扛住两万次查询
算法
小羊没烦恼!8 小时前
在Scrum中实施敏捷建模
java·开发语言·windows·算法·c#