Tree

There are 4 traversal

  1. Inorder Traversal

=> Remember this pattern

public IList<int> InorderTraversal(TreeNode root) 
{
        var res = new List<int>();
        DFS(root, res);
        return res;
    }

private void DFS(TreeNode root, List<int> res)
{
    if(root == null)
    {
        return;
    }
    DFS(root.left, res);
    res.Add(root.val);
    DFS(root.right, res);
}

Last updated