题目描述
https://leetcode-cn.com/problems/binary-tree-right-side-view/
解
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func rightSideView(root *TreeNode) []int {
var (
r []int
c int
)
if root == nil {
return []int{}
}
DFS4(root, &r, c)
return r
}
func DFS4(root *TreeNode, r *[]int, count int) {
if count >= len(*r) {
*r = append(*r, root.Val)
}
if root.Right != nil {
DFS4(root.Right, r, count+1)
}
if root.Left != nil {
DFS4(root.Left, r, count+1)
}
}
思路
一开始看题目是没看懂的,后面看题解才看出是啥意思,上面使用的是深度优先遍历!
网友评论