从0开始刷剑指Offer(30~35]

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

思路: 单调栈O(n)

class MinStack {
    Stack<Integer> stack1;
    Stack<Integer> stack2; 
    /** initialize your data structure here. */
    public MinStack() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }
    
    public void push(int x) {
        if(stack2.empty() || x <= stack2.peek()) {
            stack2.push(x);
        }
        stack1.push(x);
    }
    
    public void pop() {
        int x = stack1.pop();
        if(x == stack2.peek()) stack2.pop();
    }
    
    public int top() {
        return stack1.empty() ?  null : stack1.peek();
    }
    
    public int getMin() {
        return stack2.empty() ? null : stack2.peek();
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */

剑指 Offer 31. 包含min函数的栈

思路: (栈,模拟)

class Solution {
    Stack<Integer> stack = new Stack<>();
    public boolean validateBookSequences(int[] putIn, int[] takeOut) {
        int i = 0;

        for(int num : putIn) {
            stack.push(num);
            while(!stack.empty() && stack.peek() == takeOut[i]) {
                i ++;
                stack.pop();
            }
        }
        return stack.empty();
    }

}

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

思路一:(BFS)O(n)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int[] decorateRecord(TreeNode root) {
        if(root == null) return new int[]{};
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        ArrayList<Integer> ans = new ArrayList<>();
        while(!queue.isEmpty()) {
            TreeNode node = queue.poll();
            ans.add(node.val);
            if(node.left != null) queue.add(node.left);
            if(node.right != null) queue.add(node.right);
        }
        int[] res = new int[ans.size()];
        for(int i = 0; i < ans.size(); i ++) {
            res[i] = ans.get(i);
        }
        return res;
    }
}

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

思路一:(BFS)O(n)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> decorateRecord(TreeNode root) {   
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        if(root != null) queue.add(root);
        while(!queue.isEmpty()) {
            List<Integer> tmp = new ArrayList<>();
            for(int i = queue.size(); i > 0; i --) {
                TreeNode node = queue.poll();
                tmp.add(node.val);
                if(node.left != null)  queue.add(node.left);
                if(node.right != null) queue.add(node.right);
            }
                     res.add(tmp);
        }
        return res;
    }
}

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

思路一:(BFS)O(n)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> decorateRecord(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        if(root != null) queue.add(root);
        int flag = 1;
        while(!queue.isEmpty()) {
            List<Integer> tmp = new ArrayList<>();
            for(int i = queue.size(); i > 0; i --) {
                TreeNode node = queue.poll();
                tmp.add(node.val);
                if(node.left != null) queue.add(node.left);
                if(node.right != null) queue.add(node.right);
            }
            if(flag % 2 == 0) {
                Collections.reverse(tmp);
            }
            flag ++;
            res.add(tmp);
        }
        return res;
    }
}

思路二:双端队列O(n)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> decorateRecord(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        if(root != null) queue.add(root);
        while(!queue.isEmpty()) {
            LinkedList<Integer> tmp = new LinkedList<>();
            for(int i = queue.size(); i > 0; i --) {
                TreeNode node = queue.poll();
                if(res.size() % 2 == 0) tmp.addLast(node.val);
                else tmp.addFirst(node.val);
                if(node.left != null) queue.add(node.left);
                if(node.right != null) queue.add(node.right);
            }
            res.add(tmp);
        }
        return res;
    }
}

剑指 Offer 33. 二叉搜索树的后序遍历序列

思路一:递归O(n2)

class Solution {
    public boolean verifyTreeOrder(int[] postorder) {
        return recur(postorder, 0, postorder.length - 1);
    }

    private boolean recur(int[] postorder, int left, int right) {
        if(left >= right) return true;
        int root = postorder[right];
        int index = left;
        while(postorder[index] < root) {
            index ++;
        } //节点左分支
        int m = index; //第一个大于root节点的值
        while(index < right) {
            if(postorder[index] < root) {
                return false;
            }
            index ++;
        }
        return recur(postorder, left, m - 1) && recur(postorder, m, right - 1);
    }
}

剑指 Offer 34. 二叉树中和为某一值的路径

思路: (前序遍历,递归)O(n2)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    LinkedList<List<Integer>> res = new LinkedList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> pathTarget(TreeNode root, int target) {
        recur(root, target);
        return res;
    }

    private void recur(TreeNode root, int tar) {
    if(root == null) return;
    path.add(root.val);
    tar -= root.val;
    if(tar == 0 && root.left == null && root.right == null) {
        res.add(new LinkedList(path));
    }
    recur(root.left, tar);
    recur(root.right, tar);
    path.removeLast();   //递归移除最后的叶子节点
    }
}

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

思路: (迭代)O(n)

/*
// Definition for a Node.
class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
*/
class Solution {
    public Node copyRandomList(Node head) {
        if(head == null) return null;
        Node dummy = head;
        while(head != null) {
            Node cur = new Node(head.val);
            cur.next = head.next;
            head.next = cur; 
            head = cur.next;
        }
        head = dummy;

        while(head != null) {
            if(head.random != null) {
            head.next.random = head.random.next;
                }
            head = head.next.next;
        }
        head = dummy.next;
        Node pre = dummy, res = dummy.next;
        while(head.next != null) {
            pre.next = pre.next.next;
            head.next = head.next.next;
            pre = pre.next;
            head = head.next;
        }
        pre.next = null;
        return res;
    }
}
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值