package main import "fmt" type Node struct { parent *Node left *Node right *Node val int isLeft bool } func (n *Node) pushLeft(val int) { n.left = &Node{ parent: n, val: val, isLeft: true, } } func (n *Node) pushRight(val int) { n.right = &Node{ parent: n, val: val, isLeft: false, } } func (n *Node) maxDepth(depth int) int { if n == nil { return depth } return max(n.left.maxDepth(depth+1), n.right.maxDepth(depth+1)) } func (n *Node) find(target int) *Node { if n == nil { return nil } if n.val == target { return n } l := n.left.find(target) if l != nil { return l } return n.right.find(target) } func (n *Node) radial(nodes *[]*Node, distance int, fromTarget bool, fromChild bool, fromLeft bool) { if n == nil { return } if distance == 0 { *nodes = append(*nodes, n) return } if fromChild { if fromLeft { n.right.radial(nodes, distance-1, false, false, false) } else { n.left.radial(nodes, distance-1, false, false, false) } n.parent.radial(nodes, distance-1, false, true, n.isLeft) } else { n.left.radial(nodes, distance-1, false, false, false) n.right.radial(nodes, distance-1, false, false, false) } if fromTarget { n.parent.radial(nodes, distance-1, false, true, n.isLeft) } } func main() { root := Node{ val: 1, } root.pushRight(2) root.right.pushLeft(3) root.right.pushRight(4) root.pushLeft(5) root.left.pushLeft(6) root.left.pushRight(7) root.left.left.pushLeft(8) root.left.left.pushRight(9) root.left.right.pushLeft(10) root.left.right.pushRight(11) find := root.find(5) fmt.Println(find) nodes := []*Node{} find.radial(&nodes, 2, true, false, false) fmt.Println(nodes) for _, node := range nodes { fmt.Println(node.val) } }