美文网首页
LeetCode 101-105

LeetCode 101-105

作者: 1nvad3r | 来源:发表于2020-10-27 19:12 被阅读0次

101. 对称二叉树

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return check(root.left, root.right);
    }

    public boolean check(TreeNode left, TreeNode right) {
        if (left == null && right == null) {
            return true;
        }
        if (left == null || right == null || left.val != right.val) {
            return false;
        }
        return check(left.left, right.right) && check(left.right, right.left);
    }
}

102. 二叉树的层序遍历

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }
        List<List<Integer>> res = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode front = queue.poll();
                list.add(front.val);
                if (front.left != null) {
                    queue.offer(front.left);
                }
                if (front.right != null) {
                    queue.offer(front.right);
                }
            }
            res.add(new ArrayList<>(list));
        }
        return res;
    }
}

103. 二叉树的锯齿形层次遍历

class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }
        List<List<Integer>> res = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        boolean flag = true;//true正向,false反向
        while (!queue.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode front = queue.poll();
                list.add(front.val);
                if (front.left != null) {
                    queue.offer(front.left);
                }
                if (front.right != null) {
                    queue.offer(front.right);
                }
            }
            if (flag == true) {
                res.add(new ArrayList<>(list));
                flag = false;
            } else {
                Collections.reverse(list);
                res.add(new ArrayList<>(list));
                flag = true;
            }
        }
        return res;
    }
}

104. 二叉树的最大深度

class Solution {
    int res = 0;

    public void dfs(int level, TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            res = Math.max(res, level);
        }
        dfs(level + 1, root.left);
        dfs(level + 1, root.right);
    }

    public int maxDepth(TreeNode root) {
        dfs(1, root);
        return res;
    }
}

105. 从前序与中序遍历序列构造二叉树

class Solution {
    public TreeNode build(int preL, int preR, int inL, int inR, int[] preOrder, int[] inOrder) {
        if (preL > preR) {
            return null;
        }
        int rootVal = preOrder[preL];
        TreeNode root = new TreeNode(rootVal);
        int index;
        for (index = inL; index <= inR; index++) {
            if (inOrder[index] == rootVal) {
                break;
            }
        }
        int leftNum = index - inL;
        root.left = build(preL + 1, preL + leftNum, inL, inL + leftNum, preOrder, inOrder);
        root.right = build(preL + leftNum + 1, preR, index + 1, inR, preOrder, inOrder);
        return root;

    }

    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return build(0, preorder.length - 1, 0, inorder.length - 1, preorder, inorder);
    }
}

相关文章

  • LeetCode 101-105

    101. 对称二叉树[https://leetcode-cn.com/problems/symmetric-tre...

  • 101-105

    学习课程 101讲:人际沟通|有效交流的逻辑 102讲:沟通设计|交流需考虑的四要素 103讲:乔哈里窗|好东西和...

  • 诗词101-105

    20150920 101 菩萨蛮·红楼别夜堪惆怅 韦庄红楼别夜堪惆怅,香灯半卷流苏帐。残月出门时,美人和泪辞。琵琶...

  • 诗篇101-105

    诗篇 第101篇 〔大卫的诗〕 我要歌唱慈爱和公平;耶和华啊,我要向你歌颂!我要用智慧行完全的道。你几时到我这里来...

  • 诗篇101-105

    诗篇 第101篇 〔大卫的诗〕 我要歌唱慈爱和公平;耶和华啊,我要向你歌颂!我要用智慧行完全的道。你几时到我这里来...

  • 少儿编程游戏CodeMonkey通关攻略:第101-105关

    今天我们一起玩if判断语句的前5关:第101-105关。 在这5关以及后面的关卡里,我们的小猴子会有一个得力的助手...

  • 1000 lucky moments | 101-105

    May 25th. 1.至今都没好好感谢过这份工作。求职季时一直祈祷的是一份工资高,工作有挑战性,有时间做自己喜欢...

  • 英语口语 101-105

    1. hunt v. 寻找 hunt for sth 寻找某物 high and low= everywhere ...

  • 《活好2》:100多岁的老人,仍然活得像少年

    《活好2》是畅销15万册《活好》姊妹篇,记录了日本国宝医师日野原重明从101-105岁的所思所想,告诉我们如何在困...

  • week 2019-06-23

    LeetCode 16[M] LeetCode 17[M] LeetCode 926

网友评论

      本文标题:LeetCode 101-105

      本文链接:https://www.haomeiwen.com/subject/dfvzmktx.html