二叉树的创建及DFS、BFS遍历

public class BinaryTree {
    // 创建二叉树的方法
    public TreeNode createTree(int[] value) {
        //List<TreeNode> list = new ArrayList<>();
        LinkedList<TreeNode> list = new LinkedList<>();
        for (int i = 0; i < value.length; i++) {
            TreeNode node = new TreeNode(value[i]);
            list.add(node);
        }
        TreeNode root;
        for (int i = 0; i < list.size() / 2; i++) {
            root = list.get(i);
            TreeNode left = list.get(2 * i + 1);
            root.left = left;
            if (2 * i + 2 < list.size()) {
                TreeNode right = list.get(2 * i + 2);
                root.right = right;
            }
        }
        return list.get(0);
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if (root != null) {
            System.out.print(root.val + " ");
            preOrder(root.left);
            preOrder(root.right);
        }
    }

    // 中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    // 后序遍历
    public void postOrder(TreeNode root) {
        if (root != null) {
            postOrder(root.left);
            postOrder(root.right);
            System.out.print(root.val + " ");
        }
    }

    public static void main(String[] args) {
        int[] value = new int[]{1, 2, 3, 4, 5};
        BinaryTree tree = new BinaryTree();
        TreeNode root = tree.createTree(value);
        System.out.println("前序遍历:");
        tree.preOrder(root);
        System.out.println();
        System.out.println("中序遍历:");
        tree.inOrder(root);
        System.out.println();
        System.out.println("后序遍历:");
        tree.postOrder(root);
    }

}

层序遍历1

// 层序遍历:用队列实现
    public static List<Integer> levelOrder(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Deque<TreeNode> queue = new ArrayDeque<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            res.add(cur.val);
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
        return res;
    }

层序遍历2 

public static List<List<Integer>> levelOrder2(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        Deque<TreeNode> queue = new ArrayDeque<>();
        if (root != null) {
            queue.offer(root);
        }
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> result = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                result.add(cur.val);
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            res.add(result);
        }
        return res;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值