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

访问一个节点:

  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;

    }
}
相关推荐
zyl76514 小时前
FlashAttention 详解:从 GPU 硬件、普通 Attention 到分块计算与 Online Softmax
算法
爱读书的小胖18 小时前
LeetCode 122:买卖股票的最佳时机 II(C)解题分析
算法
结局无敌18 小时前
React Native for OpenHarmony 实战:三方库 react-native-modal-dialog 的鸿蒙化适配指南
深度学习·算法·react native·react.js·harmonyos
OPEN-F18 小时前
ROS2系列教程:上位机建图与导航(原理综合)
算法·机器人
weixin_3077791318 小时前
C++代码实现MATLAB中的fitrtree函数功能
开发语言·c++·算法·matlab
tomwang032219 小时前
路径优化建模、优化算法分类及工程化小结
算法·数学建模·分类
小羊没烦恼!19 小时前
Ajax与JSON的一些总结
android·java·前端·数据库·mysql·算法·okhttp
by2099919 小时前
哈希表——快速找到数据:冲突、删除与扩容的实现
数据结构·c++·哈希算法·散列表
2501_9269783319 小时前
人机通信协议--人和机器之间的交互想象
人工智能·经验分享·算法·ai写作
行者全栈架构师19 小时前
【鸿蒙心迹】互动卡片全链路——把核心功能放到桌面(FormKit 实战)(HarmonyOS 7.x)
前端·算法·架构