Skip to content

Latest commit

 

History

History
30 lines (28 loc) · 499 Bytes

binary-tree-inorder-traversal.md

File metadata and controls

30 lines (28 loc) · 499 Bytes

Code

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */

func inorderTraversal(root *TreeNode) []int {
	arr := []int{}
	stack := []*TreeNode{}
	for len(stack) != 0 || root != nil {
		if root != nil {
			stack = append(stack, root)
			root = root.Left
		} else {
			root = stack[len(stack)-1]
			stack = stack[:len(stack)-1]
			arr = append(arr, root.Val)
			root = root.Right
		}
	}
	return arr
}