【Java】二叉树的遍历总结(递归与非递归)

二叉树的前序遍历

递归:

public ArrayList<Integer> postorder(TreeNode root) {
       ArrayList<Integer> res = new ArrayList<Integer>();
       //采用递归方式
      if (root==null)
          return res;
      if (root!=null)
         res.add(root.val);
      if (root.left!=null)
      postorderTraversal(root.left);
      if (root.right!=null)
      postorderTraversal(root.right);
      return res;
}

非递归:

  ArrayList<Integer> res = new ArrayList<Integer>();
    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        //采用非递归方式:用到栈,先右子树进栈,后左子树进栈
        Stack <TreeNode> stack = new Stack<TreeNode>();
        if (root == null)
            return res;
        if (root !=null){
            stack.push(root);
            while(!stack.isEmpty()){
                TreeNode n =stack.pop();
                res.add(n.val);
                if (n.right!=null)
                    stack.push(n.right);
                if (n.left != null)
                    stack.push(n.left);
            }
        } 
        return res;
        }
    }

 二叉树的中序遍历

递归:

public ArrayList<Integer>  inorder(TreeNode root) {
       ArrayList<Integer> res = new ArrayList<Integer>();
       //采用递归方式
      if (root==null)
          return res;
      if (root.left!=null)
      postorderTraversal(root.left);
      res.add(root.val);
      if (root.right!=null)
      postorderTraversal(root.right);
      return res;
}

非递归:

public ArrayList<Integer> inorder(TreeNode root) {
       ArrayList<Integer> res = new ArrayList<>();
        if(root == null) {
             return res;
         }
       Stack<TreeNode> stack = new Stack<>();
       TreeNode p = root;
        while(p != null || !stack.isEmpty()) {
             if(p != null) {
              stack.push(p);
                 p = p.left;
          } else {
                 p = stack.pop();
                res.add(p.val);
                 p = p.right;
             }
         }
          return res;
    }

二叉树的后序遍历

递归:

public ArrayList<Integer>  postorder(TreeNode root) {
       ArrayList<Integer> res = new ArrayList<Integer>();
       //采用递归方式
      if (root==null)
          return res;
      if (root.left!=null)
      postorderTraversal(root.left);
      if (root.right!=null)
      postorderTraversal(root.right);
      res.add(root.val);
      return res;
}

非递归:

//!!!不要忘记导对应的包
public ArrayList<Integer> postorder(TreeNode root) {
       ArrayList<Integer> res = new ArrayList<Integer>();
      //非递归方式:
        if (root==null)
           return res;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode p = root;
        TreeNode r = null;
        while(p != null || !stack.isEmpty()){
            if (p !=null){
                stack.push(p);
                p = p.left;     //所有左孩子入栈,直到左孩子为空
            }
            else{
                p = stack.peek();  
                p = p.right;     //若果没有节点没有左孩子,访问栈顶元素的右孩子
                if(p !=null && p !=r){
                    stack.push(p);     //若果有右孩子,就让其顺延所有的左孩子入栈
                    p=p.left;
                }else{
                    p=stack.pop();   //若果没有右孩子,就把就出栈,访问,
                    res.add(p.val);
                    r=p;         //r记录刚访问的节点
                    p=null;      //把p置空,就可以继续访问栈顶元素
                }
            }
        }
        return res;
    }
}

猜你喜欢

转载自www.cnblogs.com/yaogungeduo/p/11245116.html