Skip to content

Instantly share code, notes, and snippets.

@EJSohn
Created April 19, 2019 15:39
Show Gist options
  • Select an option

  • Save EJSohn/8087288139bda7e18febd85540a03203 to your computer and use it in GitHub Desktop.

Select an option

Save EJSohn/8087288139bda7e18febd85540a03203 to your computer and use it in GitHub Desktop.
// c like struct
type TreeNode struct {
Value int
Left, Right *TreeNode // pointers
}
func DfsInOrder(treeNode *TreeNode) {
if treeNode == nil {
return
}
// implicit dereferencing, c's ->
// in-order: recursive traverse left, print current node value, recursive traverse right
DfsInOrder(treeNode.Left)
fmt.Println(treeNode.Value)
DfsInOrder(treeNode.Right)
}
/*
5
/ \
1 2
/
7
DFS In-Order: 1, 5, 2, 7
*/
func main() {
// get pointers to tree struct
tree := &TreeNode{5,
&TreeNode{1, nil, nil},
&TreeNode{2, &TreeNode{7, nil, nil}, nil}}
DfsInOrder(tree)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment