首页 > 其他分享 >【code基础】树的三种遍历的常规解法

【code基础】树的三种遍历的常规解法

时间:2022-10-05 14:23:19浏览次数:46  
标签:遍历 return res st code null root 解法

树的中序遍历有三种解法,包括:

  • 递归 (好理解,代码简单,但效率不高)
  • 借助栈的迭代方法
  • 莫里斯遍历

1.递归


     List<Integer> res = new ArrayList<>();
    //前序
    public List<Integer> preorderTraversal(TreeNode root) {
            if (root == null) return res;
            res.add(root.val);
            preorderTraversal(root.left);
            preorderTraversal(root.right);
            return res;
        }

    //中序
    public List<Integer> inorderTraversal(TreeNode root) {
            //递归的边界条件,返回res即对res不做操作
            if (root == null) return res;
            //保证如下顺序,一定是先遍历,再add
            inorderTraversal(root.left);
            res.add(root.val);
            inorderTraversal(root.right);

            return res;
        }

    //后序
    public List<Integer> postorderTraversal(TreeNode root) {
            //递归的边界条件,返回res即对res不做操作
            if (root == null) return res;
            //保证如下顺序,一定是先遍历,再add
            postorderTraversal(root.left);
            postorderTraversal(root.right);
            res.add(root.val);

            return res;
    }

2.借助于栈做迭代

        //前序--迭代方法-栈保存顺序
        public List<Integer> preorderTraversal2(TreeNode root) {
            if (root == null) return res;

            Stack<TreeNode> st = new Stack<>();
            st.push(root);
            while (!st.isEmpty()){
                TreeNode pop = st.pop();
                res.add(pop.val);
                //后进先出
                if (pop.right!=null) st.push(pop.right);
                if (pop.left!=null) st.push(pop.left);
            }
            return res;
        }

      // 中序--迭代方法-栈保存顺序
      public List<Integer> inorderTraversal(TreeNode root) {
            if (root == null) return res;
            Stack<TreeNode> st = new Stack<>();
            while (root!=null ||!st.isEmpty()){
                //遍历左子树
                while (root !=null){
                    st.push(root);
                    root = root.left;
                }
                //左子树遍历完了,取左子树最左下的结点,加入res
                root= st.pop();
                res.add(root.val);
                //遍历最下结点的右子树
                root = root.right;
            }
            return res;
        }

    // 后序--迭代方法-栈+被访问指针保存顺序
      public List<Integer> postorderTraversal1(TreeNode root) {
            if (root == null) return res;
            TreeNode pre = null; //已遍历过的指针
            Stack<TreeNode> st = new Stack<>();
            while (root!=null ||!st.isEmpty()){
                //遍历左子树
                while (root !=null){
                    st.push(root);
                    root = root.left;
                }
                //左子树遍历完了,取左子树最左下的结点
                root= st.pop();
                //出栈条件: 右结点为空
                if (root.right == null || root.right == pre){
                    res.add(root.val);
                    pre = root;
                    root = null;
                }
                //右子树不空的时候,根结点继续入栈,遍历右结点
                else {
                    st.push(root);
                    root = root.right;
                }
            }
            return res;
        }

标签:遍历,return,res,st,code,null,root,解法
From: https://www.cnblogs.com/xiaoyu-jane/p/16755507.html

相关文章

  • Codeforces Round #824 (Div. 2)
    题目链接A~D懒得写。不是因为题不好,其实我觉得做起来非常舒适。但就是懒得写了(E-HousePlanning\(d_1,d_2\)全打乱感觉很难。先看看不打乱怎么做。对于每个\(i......
  • LeetCode - 字符串类型题目
    剑指Offer05.替换空格请实现一个函数,把字符串 s 中的每个空格替换成"%20"。方法:双指针首先统计空格数量count,然后为原字符串数组扩展出2*count的空间,用来存......
  • leetcode 530. Minimum Absolute Difference in BST二叉搜索树的最小绝对差 (简单)
    一、题目大意给你一个二叉搜索树的根节点root,返回树中任意两不同节点值之间的最小差值。差值是一个正数,其数值等于两值之差的绝对值。示例1:输入:root=[4,2,6,1......
  • Codeforces Round #774 (Div. 2) - E. Power Board
    枚举+数论Problem-E-Codeforces题意有一个\(n*m\;(1<=n,m<=10^6)\)的矩阵,第i行第j列是\(i^j\),求这个矩阵中的\(n*m\)的数中有多少种不同的数思路......
  • Codeforces Round #785 (Div. 2) - D. Lost Arithmetic Progression
    GCDProblem-D-Codeforces题意有2个等差数列A,B,它们公有的项组成新的等差数列C;现在给出B的(首项,公差,项数)=(b,q,y),C的(首项,公差,项数)=(c,r,z)求满足条件的A的数量,如......
  • [Oracle] LeetCode 41 First Missing Positive 思维
    Givenanunsortedintegerarraynums,returnthesmallestmissingpositiveinteger.Youmustimplementanalgorithmthatrunsin\(O(n)\)timeandusesconstan......
  • 代码随想录day15 | 102.二叉树的层序遍历 226.反转二叉树 101.对称二叉树
    102.二叉树的层序遍历题目|文章1.迭代思路1.创建一个队列2.确定每一层的节点个数,对每一层进行遍历,将结果输出。实现点击查看代码classSolution{public:ve......
  • AtCoder Regular Contest 149(持续更新)
    Preface最近国庆在外面玩的有点high啊,欠了一篇AT和两篇CF没写,今天先浅浅写一下这场当时10.2号在外面玩得有点晚了,到寝室刚好比赛开始,晚饭都没吃就开干了主要是C写的太久......
  • leetcode144,94,145
    二叉树的前中后序遍历递归方式:1:确定递归函数的参数和返回值2:确定终止条件3:确定单层递归的逻辑classSolution{publicList<Integer>preorderTraversal(TreeNod......
  • VScode Remote 远程开发与调试
    简介最近VScode发布了远程编程与调试的插件RemoteDevelopment,使用这个插件可以在很多情况下代替vim直接远程修改与调试服务器上的代码,同时具备代码高亮与补全功能,就和在本......