【算法】树的遍历

二叉树的前中后 递归与非递归遍历

一、前序

  • 递归
class Solution {
    List<Integer> res;
    public List<Integer> preorderTraversal(TreeNode root) {
        res = new ArrayList<>();
        dfs(root);
        return res;
    }
    public void dfs(TreeNode root){
        if(root == null)return;
        res.add(root.val);
        dfs(root.left);
        dfs(root.right);
    }
}
  • 非递归
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
       List<Integer> res = new ArrayList<>();
       Stack<TreeNode> stk = new Stack<>();

       if(root == null)return res;
       stk.push(root);
       while(!stk.isEmpty()){
            TreeNode t = stk.pop();
            if(t != null){
                if(t.right != null)stk.push(t.right);
                if(t.left != null)stk.push(t.left);
            }
            res.add(t.val);
        }
        return res;
    }
}

二、后序

  • 递归
class Solution {
    List<Integer> res = new ArrayList<>();
    public List<Integer> postorderTraversal(TreeNode root) {
        if(root == null)return res;
        dfs(root);
        return res;
    }
    public void dfs(TreeNode root){
        if(root == null)return;
        dfs(root.left);
        dfs(root.right);
        res.add(root.val);
    }
}
  • 非递归
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stk = new Stack<>();
        Stack<Integer> help = new Stack<>();
        if(root == null)return res;
        stk.push(root);
        while(!stk.isEmpty()){
            TreeNode t = stk.pop();
            if(t != null){
                if(t.left != null)stk.push(t.left);
                if(t.right != null)stk.push(t.right);
            }
            // System.out.println(t.val);
            help.push(t.val);
        }
        while(!help.isEmpty())res.add(help.pop());
        return res;
    }   
}

三、中序

  • 递归
class Solution {
    List<Integer> res = new ArrayList<>();
    public List<Integer> inorderTraversal(TreeNode root) {
        if(root == null)return res;
        dfs(root);
        return res;
    }
    public void dfs(TreeNode root){
        if(root == null)return;
        dfs(root.left);
        res.add(root.val);
        dfs(root.right);
    }
}
  • 非递归
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stk = new Stack<>();
        TreeNode cur = root;
        if(cur == null)return res;

        while(cur != null || !stk.isEmpty()){
            if(cur != null){
                stk.push(cur);
                cur = cur.left;
            }else{
                TreeNode t = stk.pop();
                res.add(t.val);
                cur = t.right;
            }
        }
        return res;
    }
}
全部评论

相关推荐

06-26 10:08
门头沟学院 C++
北京Golang实习,一个月4700,吃住都不报,公司位置在海淀。请问友友怎么看呢?如果要租房的话有什么建议吗
码农索隆:租房肯定是合租了,剩下的钱,差不多够正常吃饭了,看看能不能学到东西吧
点赞 评论 收藏
分享
ZywOo_求职版:谁问你了....
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务