二叉树之深度优先遍历与广度优先遍历

  • 通过二叉树的前序和中序遍历确定二叉树:就是找到中序遍历中二叉树的根结点的下标,然后根据左右两子树进行递归
  • 二叉树的深度优先遍历和广度优先遍历:就是利用栈和队列进行存取元素
/*
 * @Author:Beer
 * @Date:2019-10-06
 * @Description:
 * 通过二叉树的前序和中序遍历确定二叉树:就是找到中序遍历中二叉树的根结点的下标,然后根据左右两子树进行递归
 * 二叉树的深度优先遍历和广度优先遍历:就是利用栈和队列进行存取元素
 */

import java.util.*;

class TreeNode {
    
    
    TreeNode left;
    TreeNode right;
    int val;

    public TreeNode(int val) {
    
    
        this.val = val;
    }
}

public class TestLeverTree {
    
    
    public static void main(String[] args) {
    
    
        int[] pre = {
    
    1, 2, 4, 7, 3, 5, 6, 8};
        int[] ino = {
    
    4, 7, 2, 1, 5, 3, 8, 6};
        //构造二叉树
        TreeNode root = buildTree(pre, 0, pre.length - 1, ino, 0, ino.length - 1);
        //进行二叉树的层次遍历
        levelTree(root);
        System.out.println();
        //非递归版本的深度优先遍历
        depthThree(root);
    }

    /**
     * 通过二叉树的前序、中序构造二叉树
     *
     * @param pre
     * @param ps
     * @param pe
     * @param ino
     * @param is
     * @param ie
     */
    public static TreeNode buildTree(int[] pre, int ps, int pe, int[] ino, int is, int ie) {
    
    
        if (ps > pe) {
    
    
            return null;
        }
        int val = pre[ps];
        int index = is;
        while (is < ie && ino[index] != val) {
    
    
            index++;
        }
        if (index > ie) {
    
    
            throw new RuntimeException("Illegal input");
        }
        TreeNode node = new TreeNode(val);
        // 递归构建当前根结点的左子树,左子树的元素个数:index-is+1个
        // 左子树对应的前序遍历的位置在[ps+1, ps+index-is]
        // 左子树对应的中序遍历的位置在[is, index-1]
        node.left = buildTree(pre, ps + 1, ps + index - is, ino, is, index - 1);
        // 递归构建当前根结点的右子树,右子树的元素个数:ie-index个
        // 右子树对应的前序遍历的位置在[ps+index-is+1, pe]
        // 右子树对应的中序遍历的位置在[index+1, ie]
        node.right = buildTree(pre, ps + index - is + 1, pe, ino, index + 1, ie);
        return node;
    }

    /**
     * 二叉树的层次遍历
     *
     * @param root
     */
    public static void levelTree(TreeNode root) {
    
    
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
    
    
            TreeNode node = queue.poll();
            System.out.print(node.val);
            if (node.left != null) {
    
    
                queue.add(node.left);
            }
            if (node.right != null) {
    
    
                queue.add(node.right);
            }

        }
    }

    /**
     * 二叉树的深度优先遍历
     * @param root
     */
    public static void depthThree(TreeNode root) {
    
    
        Stack<TreeNode> stack = new Stack<>();
        stack.add(root);
        while (!stack.isEmpty()) {
    
    
            TreeNode node = stack.pop();
            System.out.print(node.val);
            if (node.right != null) {
    
    
                stack.add(node.right);
            }
            if (node.left != null) {
    
    
                stack.add(node.left);
            }
        }
    }
}

结果:
12345678
12473568

猜你喜欢

转载自blog.csdn.net/Beer_xiaocai/article/details/102267569