LeetCodeday15

107. 二叉树的层序遍历 II

给你二叉树的根节点 root ,返回其节点值 自底向上的层序遍历 。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

在这里插入图片描述`
工具类

public class BiTree {

    public static TreeNode createTreeBinary(TreeNode treeNode) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        if (num == 0) {
            return null;
        } else {
            treeNode.val = num;
            treeNode.left = createTreeBinary(new TreeNode(0));
            treeNode.right = createTreeBinary(new TreeNode(0));
            return treeNode;
        }
    }

    public static void printTreeBinary(TreeNode treeNode) {
        if (treeNode != null) {
            System.out.print(treeNode.val + " ");
            printTreeBinary(treeNode.left);
            printTreeBinary(treeNode.right);
        }
    }
}
public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(int val) {
        this.val = val;
    }
}
public class Leetcode107 {
    public static void main(String[] args) {
        TreeNode tree = BiTree.createTreeBinary(new TreeNode(0));
        List<List<Integer>> lists = levelOrderBottom(tree);
        lists.forEach(list -> {
            list.forEach(l -> System.out.print(l + " "));
            System.out.println();
        });
    }

    public static List<List<Integer>> levelOrderBottom(TreeNode root) {
        Deque<TreeNode> queue = new ArrayDeque<>();
        if (root != null) {
            queue.addLast(root);
        }
        List<List<Integer>> lists = new ArrayList<>();
        while (!queue.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            int size = queue.size();
            while (size-- > 0) {
                TreeNode node = queue.removeFirst();
                list.add(node.val);
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
            lists.add(list);
        }
        int left = 0;
        int right = lists.size() - 1;
        while (left < right) {
            List<Integer> temp = lists.get(left);
            lists.set(left, lists.get(right));
            lists.set(right, temp);
            left++;
            right--;
        }
        return lists;

    }
}

101. 对称二叉树

给你一个二叉树的根节点 root , 检查它是否轴对称。
在这里插入图片描述

public class Leetcode101 {
    public static void main(String[] args) {
        TreeNode tree = BiTree.createTreeBinary(new TreeNode(0));
        System.out.println(isSymmetric(tree));
    }

    public static boolean isSymmetric(TreeNode root) {
        return compare(root.left, root.right);
    }

    public static boolean compare(TreeNode left, TreeNode right) {
        if (left == null && right != null) {
            return false;
        }
        if (left != null && right == null) {
            return false;
        }

        if (left == null && right == null) {
            return true;
        }
        if (left.val != right.val) {
            return false;
        }
        // 比较外侧
        boolean outside = compare(left.left, right.right);
        // 比较内侧
        boolean inside = compare(left.right, right.left);
        return outside && inside;
    }
}

226.翻转二叉树

给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。
在这里插入图片描述

public class Leetcode226 {
    public static void main(String[] args) {
        TreeNode tree = BiTree.createTreeBinary(new TreeNode(0));
        TreeNode treeNode = invertTree(tree);
        BiTree.printTreeBinary(treeNode);
    }

    public static TreeNode invertTree(TreeNode root) {
        invert(root);
        return root;
    }

    public static void invert(TreeNode root) {
        if (root == null) {
            return;
        }
        // swap(root.left,root.right)
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;

        invert(root.left);
        invert(root.right);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值