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

访问一个节点:

  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;

    }
}
相关推荐
老师我太想进步了20262 小时前
【数据结构-15】实现链式结构二叉树
c语言·数据结构
liulilittle3 小时前
认识麻将 —— 流水及对账
算法·游戏·lua·对账·流水·资金·玩法
xiangxueCode5 小时前
数据结构:容器适配器之优先级队列
数据结构·优先级队列
鱼很腾apoc6 小时前
【Redis】第2期 详解Redis五大数据结构:命令+C++实现、渐进式遍历与数据库管理
linux·服务器·c语言·数据结构·c++·redis·学习
郝学胜-神的一滴6 小时前
Effective Python 条款44:善用属性注解
开发语言·c++·vscode·python·算法·stl
青少儿编程课堂1 天前
第十八届蓝桥杯青少组图形化真题:校园分形雪花绘制解析(全国青少年信息素养大赛·电子学会图形化考级冲刺)
c++·python·算法·bfs·信息学竞赛
geovindu1 天前
python: KNeighborsClassifier K
开发语言·后端·python·算法
让我们一起加油好吗1 天前
【数据结构】线段树详解
数据结构·c++·算法·线段树·洛谷
路灯中转站1 天前
数据结构C语言版(什么是数据类型)
数据结构