我正在尝试在不使用附加的情况下编写中序遍历,这是我的代码
(define inorder
(lambda (tree)
(define inorder-iter
(lambda (tree list)
(if (empty-tree? tree)
list
(cons (inorder-iter (left-subtree tree)
(root tree)
(inorder-iter (right-subtree tree) list)))))
(inorder-iter tree '() )))
(define empty-tree? null?)
(define root car)
(define left-subtree cadr)
(define right-subtree caddr)
tree-1
(9
(6 (5 () ()) ())
(18 (11 () (13 () (17 () ()))) (65 (52 (41 (39 () ()) ()) ()) (99 () ()))))
当我调用 (inorder tree-1) 时,我得到 (((5 . 6) . 9) (11 13 17 . 18) (((39 . 41) . 52) . 65) 99) 这看起来非常糟糕。我想要得到的是'(5 6 9 11 13 17 18 39 41 52 65 99)。我到底做错了什么?谢谢!