Given an array of integers (0 to 9), rearrange the positions of the array elements to represent the second largest possible number.
Example 1:
Input: [1, 2, 3, 4, 5]
Output: [5, 4, 3, 1, 2]
Largest number: [5, 4, 3, 2, 1] (e.g. "54321")
Second largest: [5, 4, 3, 1, 2] (e.g. "54312")
Example 2:
Input: [1, 1, 2, 3, 4]
Largest number: [4, 3, 2, 1, 1]
Second largest: [4, 3, 1, 2, 1]
Given a binary tree, imagine yourself standing on the left side of it. Return the values of the nodes you can see ordered from bottom to top. Then switch to the right side of the tree, and return the values of the nodes you can see ordered from top to bottom.
Example:
Binary Tree:
1
/ \
2 3
/ \ /
6 5 4
Answer: [6, 2, 1, 3, 4]
这组题目考察的是“排列构造”和“二叉树视图遍历”两类常见面试思路。第一题本质上是把数组按字典序尽量做大:先得到最大排列,再在不改变整体前缀尽量大的前提下,找到右侧最靠前的可交换位置,构造出严格次大的排列。第二题则需要按左右视角分别收集二叉树能看到的节点:左侧视图要求从底到顶输出,右侧视图要求从顶到低输出,通常可以结合层序遍历或 DFS 记录每一层最先 / 最右出现的节点。