Print binary tree in BFS fashion with O(1) space

前端 未结 4 918
礼貌的吻别
礼貌的吻别 2021-01-12 12:26

I was wondering if it\'s possible to print a binary tree in breadth first order while using only O(1) space?

The difficult part is that one have to use additional sp

4条回答
  •  猫巷女王i
    2021-01-12 13:00

    I know that this is strictly not an answer to the question, but visiting the nodes of a tree in breadth-first order can be done using O(d) space, where d is the depth of the tree, by a recursive iterative deepening depth first search (IDDFS). The space is required for the stack, of course. In the case of a balanced tree, d = O(lg n) where n is the number of nodes. I honestly don't see how you'd do it in constant space without the backlinks suggested by @Charlie Martin.

提交回复
热议问题