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

访问一个节点:

  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;

    }
}
相关推荐
在猴站学算法18 分钟前
(《统计学习方法》(第2版) 李航)第2章 感知机算法
学习·算法·机器学习·统计学习
Rainmicro23 分钟前
【无标题】
java·数据库·算法
NEFU_Jyj10 小时前
牛客周赛R168
算法
likexuec17 小时前
数据结构(2)
数据结构
Navigator_Z18 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
D e re k ZH19 小时前
C++ 标准库算法全景指南
数据结构·c++·算法
冻柠檬飞冰走茶20 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
雪落漂泊20 小时前
C++算法 二分查找篇(上)
开发语言·c++·算法
LabVIEW开发1 天前
LabVIEW 连接板:28 个端子怎么排,优先级怎么定
java·算法·labview