Interview. Binary trees Aug 4, 2018 algorithms interview golang season_01 Binary trees Model type TreeNode struct { Val int Left *TreeNode Right *TreeNode } DFS Recurcive skeleton // example of max node // assume that all nodes' values are > 0 func dfs(root *TreeNode)int{ if root == nil{ // neutral for result output return 0 } // doing for both subtrees leftMax:=dfs(root.Left) rightMax:=dfs(root.Max) // CORE LOGIC GOES HERE result:=Max(root.Val, leftMax, rightMax) // CORE LOGIC ENDS HERE return result } DFS Iterative skeleton // example of max node // assume that all nodes' values are > 0 func dfs(root *TreeNode)int{ max := 0 current := root stack := []*TreeNode{} for current !=nil || len(stack)!=0{ // navigating to last Left leaf node for current parent // and pushing them to stack to process later for current !=nil{ stack = append(stack, current) current = current.Left } //poping element from stack current = stack[len(stack)-1] stack = stack[:len(stack)-1] // CORE LOGIC GOES HERE // we have an element to work with max = Max(max, current.Val) // CORE LOGIC ENDS HERE // pointing to right node now // if it doesn't exist we will pick next from stack, or exit the loop current = current.Right } return max } BFS Iterative skeleton // example of max node // assume that all nodes' values are > 0 func dfs(root *TreeNode)int{ max := 0 current := root stack := []*TreeNode{} for current !=nil || len(stack)!=0{ // navigating to last Left leaf node for current parent // and pushing them to stack to process later for current !=nil{ stack = append(stack, current) current = current.Left } //poping element from stack current = stack[len(stack)-1] stack = stack[:len(stack)-1] // CORE LOGIC GOES HERE // we have an element to work with max = Max(max, current.Val) // CORE LOGIC ENDS HERE // pointing to right node now // if it doesn't exist we will pick next from stack, or exit the loop current = current.Right } return max } Links hackerrank. Print in Reverse hackerrank. Inserting a Node Into a Sorted Doubly Linked List hackerrank. Delete duplicate-value nodes from a sorted linked list Golang Compact If if stacks = removeFromHighest(stacks); stacks == nil { return 0 } Links hackerrank. Equal Stacks Effective Go