後順(非再帰的)Pythonの前に

木は通常、スタック補助スタックを持っています。
アイデア:左ノードの高度なスタック上の木、最後のノードの各出力、ノードを見ている(右の1つのノードの中央には、左のノードにあります)。

予約限定:左、右

res=[]
tmp=[]
cur=root
while cur or tmp:
	if cur:
		tmp.append(cur)
		cur=cur.left
	else:
		cur=tmp.pop()
		res.append(cur.val)
		cur=cur.right
return res

-----------------------------------------------
先行順走査:左右

res=[]
tmp=[]
cur=root
while cur or tmp:
	if cur:
		res.append(cur.val)
		tmp.append(cur.right)
		cur=cur.left
	else:
		cur=tmp.pop()
return res

-----------------------------------------------
後順:左右(逆の順序で左右、すなわち、そのように長い左右の位置を修正する一次として、既に約において)。

res=[]
tmp=[]
cur=root
while cur or tmp:
	if cur:
		res.append(cur.val)
		tmp.append(cur.left)
		cur=cur.right
	else:
		cur=tmp.pop()
return res[::-1]
公開された69元の記事 ウォン称賛46 ビュー5249

おすすめ

転載: blog.csdn.net/qq_42738654/article/details/104300604