funcinorderTraversal(root *TreeNode) []int { result := []int{} if root != nil { if root.Left != nil { result = append(result, inorderTraversal(root.Left)...) } result = append(result, root.Val) if root.Right != nil { result = append(result, inorderTraversal(root.Right)...) } } return result }
b. Iterative solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
funcinorderTraversal(root *TreeNode) []int { result := []int{} stack := []int{} node := root for { if node != nil { // reach to the left most node and push node to stack so we can pop left child node before parent node stack = append(stack, node.Val) node = node.Left } elseiflen(stack) > 0 { node = stack[len(stack) - 1] // pop parent node out if it doesn't have left child node stack = stack[:len(stack) - 1] result = append(result, node.Val) // switch to right child node node = node.Right } else { break } } return result }
funcinorderTraversal(root *TreeNode) []int { result := []int{} current := root for current != nil { if current.Left == nil { result = append(result, current.Val) current = current.Right } else { previous := current.Left for previous.Right != nil && previous.Right != current { previous = previous.Right } if previous.Right == nil { previous.Right = current current = current.Left } if previous.Right == current { previous.Right = nil result = append(result, current.Val) current = current.Right } } } return result }
funcpreorderTraversal(root *TreeNode) []int { result := []int{} if root != nil { result = append(result, root.Val) result = append(result, preorderTraversal(root.Left)...) result = append(result, preorderTraversal(root.Right)...) } return result }
funcpreorderTraversal(root *TreeNode) []int { result := []int{} current := root for current != nil { if current.Left == nil { result = append(result, current.Val) current = current.Right } else { previous := current.Left for previous.Right != nil && previous.Right != current { previous = previous.Right } if previous.Right == nil { result = append(result, current.Val) previous.Right = current current = current.Left } if previous.Right == current { previous.Right = nil current = current.Right } } } }
funcpostorderTraversal(root *TreeNode) []int { result := []int{} if root != nil { result = append(result, postorderTraversal(root.Left)...) result = append(result, postorderTraversal(root.Right)...) result = append(result, root.Val) } return result }
b. Iterative solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
funcpostorderTraversal(root *TreeNode) []int { result := []int{} stack := []*TreeNode{root, root} forlen(stack) > 0 { node := stack[len(stack)-1] stack = stack[:len(stack)-1] if node != nil { iflen(stack) > 0 && node == stack[len(stack)-1] { // First time to visit the parent node stack = append(stack, node.Right) stack = append(stack, node.Right) stack = append(stack, node.Left) stack = append(stack, node.Left) } else { result = append(result, node.Val) // Second time to visit the parent node } } } return result }
c. Morris Traversal
The idea is we make a PreOrder traversal but right node first, and reverse the result.
funcpostorderTraversal(root *TreeNode) []int { result := []int{} current := root for current != nil { if current.Right == nil { result = append(result, current.Val) current = current.Left } else { previous := current.Right // get the right child's left most child, and point it back to parent node for previous.Left != nil && previous.Left != current { previous = previous.Left } if previous.Left == nil { result = append(result, current.Val) previous.Left = current current = current.Right } if previous.Left == current { previous.Left = nil current = current.Left } } } for i, j := 0, len(result) - 1; i < j; i, j = i + 1, j - 1 { result[i], result[j] = result[j], result[i] } return result }
funcgetDepth(node *TreeNode)int { if node != nil { left := getDepth(node.Left) right := getDepth(node.Right) if node.Left == nil { return1 + right } elseif node.Right == nil { return1 + left } elseif left > right { return1 + right } return1 + left } return0 }