小言_互联网的博客

二叉树的遍历(JAVA递归和非递归版)

390人阅读  评论(0)


若是对二叉树的基本概念有所遗忘,可参考 二叉树百度百科二叉树的先序中序后序其实指的是根的遍历顺序。

二叉树的先序遍历

二叉树的先序(也叫前序)遍历:先访问根节点(所以叫先序),再访问左子树,最后访问右子树。(必须赞一下左神的算法课,讲解递归不能更清晰!)

先序遍历递归版:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        preOrder(root,list);
        return list;
    }
    public void preOrder(TreeNode node,List<Integer> res){
        if(node!=null){
            //与中序后序递归遍历的不同之处
            res.add(node.val);
            preOrder(node.left,res);
            preOrder(node.right,res);
        }
    }
}

先序遍历非递归版(用栈实现):

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 * */

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        if(root==null)return list;
        Stack<TreeNode> stack=new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            root=stack.pop();
            list.add(root.val);
            //栈的特性先进后出,所以压栈先右后左
            if(root.right!=null){
                stack.push(root.right);
            }
            if(root.left!=null){
                stack.push(root.left);
        }
        }
        return list;
    }
}

二叉树的中序遍历

二叉树的中序遍历:先访问左子树,再访问根节点,最后访问右子树。

中序遍历递归版:

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        inOrder(root,list);
        return list;
    }
    public void inOrder(TreeNode node,List<Integer> res){
        if(node!=null){
            inOrder(node.left,res);
            //与先序后序递归遍历的不同之处
            res.add(node.val);
            inOrder(node.right,res);    
        }
    }
 }

中序遍历非递归版(栈):

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        if(root==null)return list;
        Stack<TreeNode> stack=new Stack<>();
        while(!stack.isEmpty()||root!=null){
            if(root!=null){
                stack.push(root);
                root=root.left;
            }
            else{
                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<>();
        postOrder(root,list);
        return list;        
    }
    public void postOrder(TreeNode node,List<Integer> res){
        if(node!=null){
            postOrder(node.left,res);
            postOrder(node.right,res);
            //与先序中序递归遍历不同之处
            res.add(node.val);
        }
    }
}

后序遍历非递归版(双栈):

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        if(root==null)return list;
        Stack<TreeNode> stack=new Stack<>();
        Stack<TreeNode> stackHelp=new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            root=stack.pop();
            stackHelp.push(root);
            if(root.left!=null){
                stack.push(root.left);
            }
            if(root.right!=null){
                stack.push(root.right);
            }
        }
        while(!stackHelp.isEmpty()){
            list.add(stackHelp.pop().val);
        }
       
        return list;
    }
}

二叉树的层序遍历

二叉树的层次遍历:从上到下从左到右访问所有节点。

层序遍历递归版:

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        levelOrd(res, root, 0);
        return res;
    }
     private void levelOrd(List<List<Integer>> res, TreeNode root, int depth) {
        if (root == null) return;
        if (res.size() == depth) res.add(new LinkedList<>());
        res.get(depth).add(root.val);
        levelOrd(res, root.left, depth + 1);
        levelOrd(res, root.right, depth + 1);
    }
 }

层序遍历非递归版(队列):

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> lists=new ArrayList<List<Integer>>();
        if(root==null)return lists;
        Queue<TreeNode> queue=new LinkedList<TreeNode>();
        queue.add(root);
        while(!queue.isEmpty()){
            int count=queue.size();
            List<Integer> list=new ArrayList<>();
            while(count>0){
            TreeNode out=queue.peek();
            list.add(out.val);
            queue.poll();
            if(out.left!=null) queue.add(out.left);
            if(out.right!=null) queue.add(out.right);
                count--;
            } 
            lists.add(list);
        }
        return lists;
    }
}

转载:https://blog.csdn.net/Chen_programmer/article/details/104907117
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场