/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcrightSideView(root*TreeNode)[]int{varans[]intvardfsfunc(root*TreeNode)dfs=func(root*TreeNode){ifroot==nil{return}ans=append(ans,root.Val)ifroot.Right!=nil{dfs(root.Right)}else{dfs(root.Left)}}dfs(root)returnans}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcrightSideView(root*TreeNode)[]int{// 定义 recur 为返回以 root 为根的二叉树的右视图的逆序
varrecurfunc(root*TreeNode)[]intrecur=func(root*TreeNode)[]int{ifroot==nil{return[]int{}}leftView,rightView:=recur(root.Left),recur(root.Right)iflen(rightView)>=len(leftView){rightView=append(rightView,root.Val)returnrightView}else{copy(leftView[len(leftView)-len(rightView):],rightView)leftView=append(leftView,root.Val)returnleftView}}ans:=recur(root)fori:=0;i<len(ans)/2;i++{ans[i],ans[len(ans)-1-i]=ans[len(ans)-1-i],ans[i]}returnans}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcrightSideView(root*TreeNode)[]int{varans[]intvardfsfunc(root*TreeNode,depthint)dfs=func(root*TreeNode,depthint){ifroot==nil{return}ifdepth>=len(ans){ans=append(ans,root.Val)}ans[depth]=root.Valdfs(root.Left,depth+1)dfs(root.Right,depth+1)}dfs(root,0)returnans}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcrightSideView(root*TreeNode)[]int{varans[]intifroot==nil{returnans}varqueue[]*TreeNodequeue=append(queue,root)forlen(queue)>0{cnt:=len(queue)forcnt>0{cur:=queue[0]queue=queue[1:]ifcnt==1{ans=append(ans,cur.Val)}ifcur.Left!=nil{queue=append(queue,cur.Left)}ifcur.Right!=nil{queue=append(queue,cur.Right)}cnt--}}returnans}