Created
April 19, 2019 15:39
-
-
Save EJSohn/8087288139bda7e18febd85540a03203 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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