반응형
오늘 풀 문제는 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
}
읽어주셔서 감사합니다
'Language > Go' 카테고리의 다른 글
[Golang으로 도전하는 Leetcode] Ways to Make a Fair Array (0) | 2020.11.30 |
---|---|
[Golang으로 도전하는 Leetcode] Check If Two String Arrays are Equivalent (0) | 2020.11.30 |
[Golang으로 도전하는 Leetcode] Partition Labels (0) | 2020.09.05 |
[Golang으로 도전하는 Leetcode] Repeated Substring Pattern (0) | 2020.09.04 |
[Golang으로 도전하는 Leetcode] Contains Duplicate III (0) | 2020.09.02 |