183132I32II32III24

Posted 保护眼睛

tags:

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

剑指 Offer 18. 删除链表的节点

    public ListNode deleteNode(ListNode head, int val) {
        if (head == null)
            return null;
        ListNode cur = head;
        ListNode pre = new ListNode(-1);
        if (val != head.val) {
            pre.next = head;
        } else {
            pre.next = head.next;
            return pre.next;
        }
        while (cur != null) {
            if (cur.val == val) {
                pre.next = cur.next;
                break;
            } else {
                pre = cur;
                cur = cur.next;
            }
        }

        return head;
    }

    public ListNode deleteNode2(ListNode head, int val) {
        if (head == null) return null;
        if (head.val == val) {
            return head.next;
        } else {
            head.next = deleteNode2(head.next, val);
        }
        return head;
    }

剑指 Offer 24. 反转链表

    public ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode cur = dummy;

        while (cur.next != null && cur.next.next != null) {
            ListNode node1 = cur.next;
            ListNode node2 = cur.next.next;

            cur.next = node2;
            node1.next = node2.next;
            node2.next = node1;
            cur = node1;
        }

        return dummy.next;
    }

剑指 Offer 31. 栈的压入、弹出序列

    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int i = 0;
        for (int val : pushed) {
            stack.push(val);
            while (!stack.isEmpty() && stack.peek() == popped[i]) {
                stack.pop();
                i++;
            }
        }
        return stack.isEmpty();
    }

剑指 Offer 32 - I. 从上到下打印二叉树

    public int[] levelOrder(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null)
            return tran(res);

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            res.add(cur.val);
            if (cur.left != null) {
                queue.offer(cur.left);
            }

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

        return tran(res);
    }

    public int[] tran(List<Integer> list) {
        int size = list.size();
        int[] res = new int[size];
        for (int i = 0; i < size; i++) {
            res[i] = list.get(i);
        }

        return res;
    }

剑指 Offer 32 - II. 从上到下打印二叉树

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

剑指 Offer 32 - III. 从上到下打印二叉树

    boolean flag = true;

    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) return res;
        Queue<TreeNode> queue = new LinkedList<>();

        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> list = new ArrayList<>();

            for (int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();

                list.add(cur.val);

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

                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            if (flag) {
                res.add(list);
                flag = false;
            } else {
                Collections.reverse(list);
                res.add(list);
                flag = true;
            }
        }

        return res;
    }


    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) return res;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            LinkedList<Integer> tmpList = new LinkedList<>();

            for (int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                if (res.size() % 2 != 0) {
                    tmpList.addFirst(cur.val);
                } else {
                    tmpList.addLast(cur.val);
                }
                if (cur.left != null) {
                    queue.offer(cur.left);
                }

                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            res.add(tmpList);
        }

        return res;
    }

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

PTA:7-129 文件传输 (25分)--(并查集)

剑指 Offer 32 - III. 从上到下打印二叉树 III(java解题)

剑指offer32-III从上到下打印二叉树

LeetCode1482. 制作 m 束花所需的最少天数 / 剑指 Offer 32 . 从上到下打印二叉树I/II/III

LeetCode只出现一次的数字i/ii/iii

LeetCode只出现一次的数字i/ii/iii