노트 제목 leetcode 브러시 (python3) -. 144 이진 트리 예약 주문 순회

(144) 이진 트리 예약 주문 순회

이진 트리 감안할 때, 그 노드의 값의 전순 주사를 반환합니다.

예:

입력 : [1, NULL, 2,3]
1

2
/
3

출력 : [1,2,3]
후속 : 재귀 해결책은 간단하다 당신이 반복적으로 그것을 할 수 있을까?

두 가지 방법으로 반복과 재귀가 있습니다

func preorderTraversal(root *TreeNode) []int {
	if root == nil {
        return []int{}
	}
	res := []int{}
	preOrder(root, &res)
	return res
}

func preOrder(root *TreeNode, ans *[]int) {
	if root == nil {
		return
	}
	*ans = append(*ans, root.Val)
	preOrder(root.Left, ans)
	preOrder(root.Right, ans)
}
게시 98 개 원래 기사 · 원의 칭찬 0 · 조회수 1452

추천

출처blog.csdn.net/weixin_44555304/article/details/104427052