28、36、68

剑指 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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值