반응형

오늘 풀 문제는 All Elements in Two Binary Search Trees라는 문제이며

링크는 https://leetcode.com/problems/all-elements-in-two-binary-search-trees/입니다

이 문제는 각각 주어진 구조체를 탐색하여 배열에 넣은 뒤 정렬하면 되는 문제였습니다

코드는 아래와 같습니다

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func makeList(root *TreeNode) []int{
    if root == nil {
        return nil
    }
    result := append([]int{}, root.Val)
    res1 := makeList(root.Left)
    res2 := makeList(root.Right)
    result = append(result, res1...)
    result = append(result, res2...)
    return result
}
func getAllElements(root1 *TreeNode, root2 *TreeNode) []int {
    result_left := makeList(root1)
    result_right := makeList(root2)
    result := append(result_left, result_right...)
    sort.Ints(result)
    return result
}

읽어주셔서 감사합니다

  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기