class PreorderWithConstantSpace {

    public static List<TreeNode> solve(TreeNode root) {
        List<TreeNode> res = new ArrayList<>();
        TreeNode p = root;
        while (p != null) {
            if (p.left == null) {
                res.add(p);
                p = p.right;
                continue;
            }
            boolean add = addOrRemoveLinkToParent(p, p.left);
            if (add) {
                res.add(p);
                p = p.left;
            } else {
                p = p.right;
            }
        }
        return res;
    }

}