283668

Posted 保护眼睛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了283668相关的知识,希望对你有一定的参考价值。

剑指 Offer 28. 对称的二叉树

    //1.
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return false;
        return isSymmetricHelper(root.left, root.right);
    }

    private boolean isSymmetricHelper(TreeNode left, TreeNode right) {
        if (left == null && right == null) return true;

        if (left == null && right != null || right == null && left != null) return false;

        if (left.val != right.val) return false;

        return isSymmetricHelper(left.left, right.right) && isSymmetricHelper(left.right, right.left);
    }
    //2.
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        return helper(root.left, root.right);
    }

    public boolean helper(TreeNode left, TreeNode right) {
        Queue<TreeNode> leftQueue = new LinkedList<>();
        Queue<TreeNode> rightQueue = new LinkedList<>();

        leftQueue.offer(left);
        rightQueue.offer(right);

        while (!leftQueue.isEmpty()) {

            TreeNode curLeft = leftQueue.poll();
            TreeNode curRight = rightQueue.poll();
            if (curLeft == null && curRight == null) {
                continue;
            }
            if (curLeft != null && curRight == null || curLeft == null && curRight != null) {
                return false;
            }
            if (curLeft.val != curRight.val) {
                return false;
            }
            leftQueue.offer(curLeft.left);
            rightQueue.offer(curRight.right);
            leftQueue.offer(curLeft.right);
            rightQueue.offer(curRight.left);
        }

        return true;
    }
    //3.
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        return helper(root.left, root.right);
    }

    public boolean helper(TreeNode left, TreeNode right) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(left);
        queue.offer(right);

        while (!queue.isEmpty()) {
            TreeNode curLeft = queue.poll();
            TreeNode curRight = queue.poll();
            if (curLeft == null && curRight == null) {
                continue;
            }
            if (curLeft != null && curRight == null || curLeft == null && curRight != null) {
                return false;
            }
            if (curLeft.val != curRight.val) {
                return false;
            }
            queue.offer(curLeft.left);
            queue.offer(curRight.right);
            queue.offer(curLeft.right);
            queue.offer(curRight.left);
        }
        return true;
    }

剑指 Offer 36. 二叉搜索树与双向链表

    //1.
    Node pre, head;

    public Node treeToDoublyList(Node root) {
        if (root == null) return null;
        dfs(root);
        head.left = pre;
        pre.right = head;
        return head;
    }

    public void dfs(Node cur) {
        if (cur == null) return;
        dfs(cur.left);
        if (pre != null) pre.right = cur;
        else head = cur;
        cur.left = pre;
        pre = cur;
        dfs(cur.right);
    }
    //2.
    Queue<Node> queue = new LinkedList<>();

    public Node treeToDoublyList(Node root) {
        if (root == null) return null;
        put(root);
        Node cur = queue.poll();
        Node head = cur;
        while (!queue.isEmpty()) {
            Node next = queue.poll();
            cur.right = next;
            next.left = cur;
            cur = next;
        }
        head.left = cur;
        cur.right = head;
        return head;
    }

    public void put(Node root) {
        if (root == null) return;
        put(root.left);
        queue.offer(root);
        put(root.right);
    }
    //3.
    Queue<Node> queue = new LinkedList<>();

    public Node treeToDoublyList(Node root) {
        if (root == null) return null;
        inorder(root);
        Node cur = queue.poll();
        Node head = cur;
        while (!queue.isEmpty()) {
            Node next = queue.poll();
            cur.right = next;
            next.left = cur;
            cur = next;
        }
        head.left = cur;
        cur.right = head;
        return head;
    }

    public void inorder(Node root) {
        Stack<Node> stack = new Stack<>();
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            if (!stack.isEmpty()) {
                root = stack.pop();
                queue.offer(root);
                root = root.right;
            }
        }
    }

剑指 Offer 68 - II. 二叉树的最近公共祖先、剑指 Offer 68 - I. 二叉搜索树的最近公共祖先

    //II、I.
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) return null;
        if (p == root || q == root) return root;
        TreeNode pORq1 = lowestCommonAncestor(root.left, p, q);
        TreeNode pORq2 = lowestCommonAncestor(root.right, p, q);

        if (pORq1 != null && pORq2 != null) return root;
        if (pORq1 == null && pORq2 != null) return pORq2;
        if (pORq1 != null && pORq2 == null) return pORq1;
        return null;
    }
    //I
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root.val > p.val && root.val > q.val) {
            return lowestCommonAncestor(root.left, p, q);
        } else if (root.val < p.val && root.val < q.val) {
            return lowestCommonAncestor(root.right, p, q);
        }
        return root;
    }
    //I
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        while (root != null) {
            if (root.val > p.val && root.val > q.val) {
                root = root.left;
            } else if (root.val < p.val && root.val < q.val) {
                root = root.right;
            } else break;
        }
        return root;
    }

以上是关于283668的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数