题解 | #输出二叉树的右视图#
输出二叉树的右视图
https://www.nowcoder.com/practice/c9480213597e45f4807880c763ddd5f0
package main /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 求二叉树的右视图 * @param xianxu int整型一维数组 先序遍历 * @param zhongxu int整型一维数组 中序遍历 * @return int整型一维数组 */ func solve(xianxu []int, zhongxu []int) []int { ma := make(map[int]int) for i,val := range zhongxu{ ma[val] = i } return doSolve(xianxu,0,ma,make([]int,0),0,len(zhongxu)-1,0) // write code here } func doSolve(pre[]int,curP int,ma map[int]int,res[]int,st int ,en int ,curLen int)[]int{ if st > en { return res } root := pre[curP] if curLen >= len(res) { res = append(res,root) } mid := ma[root] res = doSolve(pre,curP+1+mid-st,ma,res,mid+1,en,curLen+1) res = doSolve(pre,curP+1,ma,res,st,mid-1,curLen+1) return res }