Second Minimum Node In a Binary Tree
Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly
zero sub-node. If the node has two sub-nodes, then this node’s value is the smaller value among its two sub-nodes.
Given such a binary tree, you need to output the second minimum value in the set made of all the nodes’ value in the whole tree.
If no such second minimum value exists, output -1 instead.
Input: 2 / \ 2 5 / \ 5 7 Output: 5 Explanation: The smallest value is 2, the second smallest value is 5.
Input: 2 / \ 2 2 Output: -1 Explanation: The smallest value is 2, but there isn't any second smallest value.
Trim a Binary Search Tree
Given a binary search tree and the lowest and highest boundaries as
R, trim the tree so that all its elements lies in
[L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.
Input: 1 / \ 0 2 L = 1 R = 2 Output: 1 \ 2
Input: 3 / \ 0 4 \ 2 / 1 L = 1 R = 3 Output: 3 / 2 / 1
Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get.
Input: 2736 Output: 7236 Explanation: Swap the number 2 and the number 7.
Input: 9973 Output: 9973 Explanation: No swap.
- The given number is in the range [0, 10^8]
Bulb Switcher II
There is a room with
n lights which are turned on initially and 4 buttons on the wall. After performing exactly
m unknown operations towards buttons, you need to return how many different kinds of status of the
n lights could be.
n lights are labeled as number [1, 2, 3, …, n], function of these 4 buttons are given below:
- Flip all the lights.
- Flip lights with even numbers.
- Flip lights with odd numbers.
- Flip lights with (3k + 1) numbers, k = 0, 1, 2, …
Input: n = 1, m = 1. Output: 2 Explanation: Status can be: [on], [off]
Input: n = 2, m = 1. Output: 3 Explanation: Status can be: [on, off], [off, on], [off, off]
Input: n = 3, m = 1. Output: 4 Explanation: Status can be: [off, on, off], [on, off, on], [off, off, off], [off, on, on].
m both fit in range [0, 1000].