import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 求二叉树的右视图
* @param xianxu int整型一维数组 先序遍历
* @param zhongxu int整型一维数组 中序遍历
* @return int整型一维数组
*/
public int[] solve (int[] xianxu, int[] zhongxu) {
// write code here
TreeNode root = reConstructBinaryTree(xianxu, zhongxu);
List<Integer> list = fromRight(root);
int[] ans = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
ans[i] = list.get(i);
}
return ans;
}
public TreeNode reConstructBinaryTree(int[] pre, int[] vin) {
if (pre.length == 0) {
return null;
}
if (pre.length == 1) {
return new TreeNode(pre[0]);
}
if (pre.length == 2) {
TreeNode root = new TreeNode(pre[0]);
if (pre[0] == vin[0]) {
root.right = new TreeNode(vin[1]);
} else {
root.left = new TreeNode(vin[0]);
}
return root;
}
int rootVal = pre[0];
int rootIndex = 0;
for (int i = 0; i < vin.length; i++) {
if (vin[i] == rootVal) {
rootIndex = i;
break;
}
}
TreeNode root = new TreeNode(rootVal);
if (rootIndex == 0) {
int[] rightVin = Arrays.copyOfRange(vin, 1, vin.length);
Map<Integer, Integer> map = new HashMap<>();
for (int i : rightVin) {
map.put(i, 0);
}
int endIndex = 0;
for (int j = 0; j < pre.length; j++) {
if (map.containsKey(pre[j])) {
endIndex = j;
}
}
int[] rightPre = Arrays.copyOfRange(pre, 1, endIndex + 1);
root.right = reConstructBinaryTree(rightPre, rightVin);
} else if (rootIndex == 1) {
root.left = new TreeNode(vin[0]);
int[] rightVin = Arrays.copyOfRange(vin, 2, vin.length);
Map<Integer, Integer> map = new HashMap<>();
for (int i : rightVin) {
map.put(i, 0);
}
int endIndex = 0;
for (int j = 0; j < pre.length; j++) {
if (map.containsKey(pre[j])) {
endIndex = j;
}
}
int[] rightPre = Arrays.copyOfRange(pre, 2, endIndex + 1);
root.right = reConstructBinaryTree(rightPre, rightVin);
} else if (rootIndex == vin.length - 1) {
int[] leftVin = Arrays.copyOfRange(vin, 0, vin.length - 1);
Map<Integer, Integer> map = new HashMap<>();
for (int i : leftVin) {
map.put(i, 0);
}
int endIndex = 0;
for (int j = 0; j < pre.length; j++) {
if (map.containsKey(pre[j])) {
endIndex = j;
}
}
int[] leftPre = Arrays.copyOfRange(pre, 1, endIndex + 1);
root.left = reConstructBinaryTree(leftPre, leftVin);
} else if (rootIndex == vin.length - 2) {
root.right = new TreeNode(vin[vin.length - 1]);
int[] leftVin = Arrays.copyOfRange(vin, 0, vin.length - 2);
Map<Integer, Integer> map = new HashMap<>();
for (int i : leftVin) {
map.put(i, 0);
}
int endIndex = 0;
for (int j = 0; j < pre.length; j++) {
if (map.containsKey(pre[j])) {
endIndex = j;
}
}
int[] leftPre = Arrays.copyOfRange(pre, 1, endIndex + 1);
root.left = reConstructBinaryTree(leftPre, leftVin);
} else {
int[] leftVin = Arrays.copyOfRange(vin, 0, rootIndex);
int[] rightVin = Arrays.copyOfRange(vin, rootIndex + 1, vin.length);
Map<Integer, Integer> map = new HashMap<>();
for (int i : leftVin) {
map.put(i, 0);
}
int endIndex = 0;
for (int j = 0; j < pre.length; j++) {
if (map.containsKey(pre[j])) {
endIndex = j;
}
}
int[] leftPre = Arrays.copyOfRange(pre, 1, endIndex + 1);
int[] rightPre = Arrays.copyOfRange(pre, endIndex + 1, pre.length);
root.left = reConstructBinaryTree(leftPre, leftVin);
root.right = reConstructBinaryTree(rightPre, rightVin);
}
return root;
}
public List<Integer> fromRight(TreeNode root) {
Map<Integer, Integer> map = new HashMap<>();
Queue<TreeNode> treeQueue = new LinkedList<>();
Queue<Integer> heightQueue = new LinkedList<>();
int maxHeight = 0;
treeQueue.add(root);
heightQueue.add(maxHeight);
while (!treeQueue.isEmpty()) {
TreeNode node = treeQueue.poll();
int height = heightQueue.poll();
maxHeight = Math.max(maxHeight, height);
if (!map.containsKey(height)) {
map.put(height, node.val);
}
if (node.right != null) {
treeQueue.add(node.right);
heightQueue.add(height + 1);
}
if (node.left != null) {
treeQueue.add(node.left);
heightQueue.add(height + 1);
}
}
List<Integer> ans = new ArrayList<>();
for (int i = 0; i <= maxHeight; i++) {
ans.add(map.get(i));
}
return ans;
}
}