3. 二叉树

概念

二叉树(binary tree)是指树中节点的度不大于2的有序树
在这里插入图片描述

1. 遍历二叉树,先序,中序,后序(递归序)

先序(头左右),中序(左头右),后序(左右头)

    // 递归遍历二叉树
    public static void recursionLoop(Node node) {
        if (node == null) {
            return;
        }
        // System.out.print(node.value); // 先序
        recursionLoop(node.left);
        System.out.print(node.value); // 中序
        recursionLoop(node.right);
        // System.out.print(node.value); // 后序
    }

非递归遍历

    // 先序遍历, while循环做法
    public static String loopPre(TNode node) {
        if (node == null) {
            return "";
        }
        Stack<TNode> stack = new Stack<>();
        stack.push(node);
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()) {
            node = stack.pop();
            sb.append(node.value);
            if (node.right != null) {
                stack.push(node.right);
            }
            if (node.left != null) {
                stack.push(node.left);
            }
        }
        return sb.toString();
    }

    // 后序遍历, while循环做法, 先用先序遍历,不打印,先左后右,放到stack2中。最后顺序打印stack2.pop()
    public static String loopSuffix(TNode node) {
        if (node == null) {
            return "";
        }
        Stack<TNode> stack = new Stack<>();
        Stack<TNode> stack2 = new Stack<>();
        stack.push(node);
        while (!stack.isEmpty()) {
            node = stack.pop();
            stack2.push(node);
            if (node.left != null) {
                stack.push(node.left);
            }
            if (node.right != null) {
                stack.push(node.right);
            }
        }
        StringBuilder sb = new StringBuilder();
        while (!stack2.isEmpty()) {
            sb.append(stack2.pop().value);
        }
        return sb.toString();
    }

    // 1. 先把左树的左边界全部放栈里面,直到 null
    // 2. 从栈中弹出节点打印,让这个节点的右孩子成为 curr ,重复第一步
    public static String loopIn(TNode node) {
        if (node == null) {
            return "";
        }
        Stack<TNode> stack = new Stack<>();
        TNode curr = node;
        StringBuilder sb = new StringBuilder();
        while (curr != null || !stack.isEmpty()) {
            if (curr != null) {
                stack.push(curr);
                curr = curr.left;
            } else {
                TNode p = stack.pop();
                sb.append(p.value);
                curr = p.right;
            }
        }
        return sb.toString();
    }

    // 递归写法
    public static String loop(TNode node, int sort) {
        StringBuilder sb = new StringBuilder();
        loop(node, sort, sb);
        return sb.toString();
    }

    public static void loop(TNode node, int sort, StringBuilder sb) {
        if (node == null) {
            return;
        }
        if (sort == 1) {
            sb.append(node.value);
        }
        loop(node.left, sort, sb);
        if (sort == 2) {
            sb.append(node.value);
        }
        loop(node.right, sort, sb);
        if (sort == 3) {
            sb.append(node.value);
        }
    }
    // 按层遍历
    public String loopLevel(TNode node) {
        if (node == null) {
            return "";
        }
        Queue<TNode> queue = new LinkedList<>();
        queue.add(node);
        StringBuilder sb = new StringBuilder();
        while (!queue.isEmpty()) {
            TNode c = queue.poll();
            sb.append(c.value);
            if (c.left != null) {
                queue.add(c.left);
            }
            if (c.right != null) {
                queue.add(c.right);
            }
        }
        return sb.toString();
    }
    @Test
    public void test1() {
        for (int i = 0; i < 10000; i++) {
            TNode tree = Reduce.binaryTree(5, 100);
            String s1 = loopPre(tree);
            String s2 = loop(tree, 1);
            if (!s1.equals(s2)) {
                System.out.println(s1);
                System.out.println(s2);
                return;
            }
        }
    }
    @Test
    public void test2() {
        for (int i = 0; i < 10000; i++) {
            TNode tree = Reduce.binaryTree(5, 100);
            String s1 = loopIn(tree);
            String s2 = loop(tree, 2);
            if (!s1.equals(s2)) {
                System.out.println(s1);
                System.out.println(s2);
                return;
            }
        }
    }
    @Test
    public void test3() {
        for (int i = 0; i < 10000; i++) {
            TNode tree = Reduce.binaryTree(5, 100);
            String s1 = loopSuffix(tree);
            String s2 = loop(tree, 3);
            if (!s1.equals(s2)) {
                System.out.println(s1);
                System.out.println(s2);
                return;
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值