26、29、35

剑指 Offer 26. 树的子结构

思路一就是递归啊、不必将递归想的太清楚、交给系统的堆栈去完成🤣、首先要判断两棵树是不是相等的:如果当前的B的节点的值等于A节点的值的话、那么就分别去看B的左和A的左是不是相等、看B的右和A的右是不是相等、直到B为null的时候、说明A和B两棵树相等、如果中途A走完或者A的值不等于B的值的话、那么返回false。
B是A的子树就是要把A的每一个节点作为根和B去判断是不是相等的、先判断A的根、再是A的left、再是A的right、如果有一个可以找到就返回true.
思路二就是bfs、思路和上面的思路是一样的、只不过是树遍历的方式b不同罢了、只是借助队列实现了树的遍历而已.

    //思路一
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (A == null || B == null) return false;

        return isSameTree(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
    }

    public boolean isSameTree(TreeNode A, TreeNode B) {
        if (B == null) return true;

        if (A == null || A.val != B.val) return false;

        return isSubTree(A.left, B.left) && isSubTree(A.right, B.right);
    }


    //思路二
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (A == null || B == null) return false;
        Queue<TreeNode> queue = new LinkedList<>();

        queue.offer(A);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur.val == B.val) {
                if (helper(cur, B)) {
                    return true;
                }
            }

            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }

        return false;
    }

    public boolean helper(TreeNode A, TreeNode B) {
        Queue<TreeNode> queueA = new LinkedList<>();
        Queue<TreeNode> queueB = new LinkedList<>();
        queueA.offer(A);
        queueB.offer(B);

        while (!queueB.isEmpty()) {
            TreeNode curB = queueB.poll();
            TreeNode curA = queueA.poll();
            if (curA == null || curA.val != curB.val) {
                return false;
            }

            if (curB.left != null) {
                queueA.offer(curA.left);
                queueB.offer(curB.left);
            }
            if (curB.right != null) {
                queueA.offer(curA.right);
                queueB.offer(curB.right);
            }
        }

        return true;
    }

剑指 Offer 29. 顺时针打印矩阵

思路就是顺时针打印、要注意上下左右四个方向、不能越界、每次打印完一个循环的时候、对应的左边界、右边界、上边界、或者下边界其中一个会增加或者减少、如果左边界大于右边界的话或者上边界大于下边界的话、那么也就遍历完数组了。

    public int[] spiralOrder(int[][] matrix) {
        int left = 0;
        int right = matrix[0].length - 1;
        int top = 0;
        int button = matrix.length - 1;
        int[] res = new int[(right + 1) * (button + 1)];
        int resIndex = 0;
        while (true) {
            for (int i = left; i <= right; i++) {
                res[resIndex++] = matrix[top][i];
            }
            if (++top > button) break;
            for (int i = top; i <= button; i++) {
                res[resIndex++] = matrix[i][right];
            }
            if (--right < left) break;
            for (int i = right; i >= left; i--) {
                res[resIndex++] = matrix[button][i];
            }
            if (--button < top) break;
            for (int i = button; i >= top; i--) {
                res[resIndex++] = matrix[i][left];
            }
            if (++left > right) break;
        }

        return res;
    }

剑指 Offer 35. 复杂链表的复制

思路一:将节点存储在hash 表中、键和值都是自己、复制的时候、就可以取出对应的值了。
思路二:三次遍历、第一次在原有的节点的后面加上本身复制的节点、原有节点的next是自己的复制节点、自己的复制节点的next是自己的next,第二次遍历将复制节点的random 指向原节点的random.next、因为当前的节点的next都是自己的复制节点、第三次的遍历就是将所有的复制节点连接起来、pre为head、cur为head.next、pre.next = pre.next.next; cur.next = cur.next.next; pre = pre.next;cur = cur.next;这样就将所有的复制节点串联起来了、最后将复制链表的next置为null.

    //思路一
    public Node copyRandomList(Node head) {
        if (head == null) return null;
        Node cur = head;
        HashMap<Node, Node> hashMap = new HashMap<>();
        while (cur != null) {
            hashMap.put(cur, new Node(cur.val));
            cur = cur.next;
        }
        cur = head;
        while (cur != null) {
            hashMap.get(cur).next = hashMap.get(cur.next);
            hashMap.get(cur).random = hashMap.get(cur.random);
            cur = cur.next;
        }

        return hashMap.get(head);
    }

    //思路二
    public Node copyRandomList(Node head) {
        if (head == null) return null;
        Node cur = head;
        while (cur != null) {
            Node newCur = new Node(cur.val);
            newCur.next = cur.next;
            cur.next = newCur;
            cur = newCur.next;
        }

        cur = head;
        while (cur != null) {
            if (cur.random != null) {
                cur.next.random = cur.random.next;
            }
            cur = cur.next.next;

        }

        Node pre = head;
        Node res = head.next;
        cur = head.next;
        while (cur.next != null) {
            pre.next = pre.next.next;
            cur.next = cur.next.next;
            pre = pre.next;
            cur = cur.next;
        }

        pre.next = null;
        return res;
    }
  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值