postorder (비 재귀) 파이썬 전

나무는 일반적으로 스택 보조 스택을 가지고있다.
아이디어 : 왼쪽 노드 고급 스택에 트리, 마지막 노드의 각 출력 노드보고 (오른쪽에 하나 개의 노드의 중간에 왼쪽 노드에 ).

예약 주문 : 왼쪽, 오른쪽

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

-----------------------------------------------
postorder : 오른쪽과 왼쪽 (즉, 오른쪽 역순으로 남아 있지만 이미 약, 너무 오래 왼쪽과 오른쪽의 위치를 수정하는 첫 번째 순서로.)

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