二叉树的遍历算法

我们都知道二叉树是一种数据结构,它有四种遍历方式,前序遍历,中序遍历,后序遍历和层次遍历。

前序遍历首先访问根节点,然后遍历左子树,最后遍历右子树。

中序遍历是先遍历左子树,然后访问根节点,然后遍历右子树。

后序遍历是先遍历左子树,然后遍历右子树,最后访问树的根节点。

层序遍历就是逐层遍历树结构。

那么他们的遍历算法怎么实现呢?

前序遍历,中序和后序遍历一般可以通过递归和迭代两种方式实现,思路基本相同,主要考虑遍历的先后问题。
递归容易理解,迭代得使用栈来帮我们保持数据。

前序遍历递归

定义 preorder(root) 表示当前遍历到 root 节点的答案。按照定义,我们只要首先将 root 节点的值加入答案,然后递归调用 preorder(root.left) 来遍历 root 节点的左子树,最后递归调用 preorder(root.right) 来遍历 root 节点的右子树即可,递归终止的条件为碰到空节点。

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        preorder(list, root);
        return list;

       
    }
     public void preorder(List<Integer> list, TreeNode root) {
            if(root == null) return;
            list.add(root.val);
            preorder(list, root.left);
            preorder(list, root.right);
            
        }
}

前序遍历迭代

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        preorder(res, root);
        return res;   
    }

    public void preorder(List<Integer> res, TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        if(root == null) return;
        while(!stack.isEmpty() || root != null) {
            while(root != null){
                res.add(root.val);
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            root = root.right;
        }
    }
}

中序遍历递归

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {

        List<Integer> list = new ArrayList<>();
        preorder(list,root);
        return list;
    }

    public void preorder(List<Integer> list, TreeNode root) {
        if(root == null) return;

        preorder(list, root.left);
        list.add(root.val);
        preorder(list, root.right);
        
    }
}

中序遍历迭代

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();

        while(!stack.isEmpty() || root != null) {
            while(root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            list.add(root.val);
            root = root.right;
        }
        return list;
    }
}

后续遍历递归

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode res = root.left;
        preorder(list,stack,res);

        return list;
    }
    public void preorder(List<Integer> list, Stack<TreeNode> stack, TreeNode root) {
        while(!stack.isEmpty() || root != null) {
            while(root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            list.add(root.val);
            root = root.right;
        }
    }
}

后续遍历迭代

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
      List<Integer> list = new ArrayList<>();
      Stack<TreeNode> stack = new Stack<>();

    TreeNode prev = null;
      while(!stack.isEmpty() || root != null) {
          while(root != null) {
              stack.push(root);
              root = root.left;
          }
          root = stack.pop();
          if(root.right == null || root.right == prev) {
              list.add(root.val);
              prev = root;
              root = null;
          }else{
              stack.push(root);
              root = root.right;
          }
      }
      return list;  
    }
}

层次遍历
首先根元素入队
当队列不为空的时候
求当前队列的长度 s
依次从队列中取 s个元素进行拓展,然后进入下一次迭代

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> list = new ArrayList<>();
        if(root == null) return list;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            List<Integer> newList = new ArrayList<>();
            int len = queue.size();
            for(int i = 0; i < len; i++) {
                root = queue.poll();
                newList.add(root.val);
                if(root.left != null) {
                    queue.offer(root.left);
                }
                if(root.right != null) {
                    queue.offer(root.right);
                }
            }
            list.add(newList);
        }
        return list;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值