美文网首页
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

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