All Elements in Two Binary Search Trees Solutions in Go
Number 1305
Difficulty Medium
Acceptance 76.2%
Link LeetCode
Other languages C++
Solutions
Go solution by halfrost/LeetCode-Go
package leetcodeimport ("sort")import ("github.com/halfrost/LeetCode-Go/structures")// TreeNode definetype TreeNode = structures.TreeNode/*** Definition for a binary tree node.* type TreeNode struct {* Val int* Left *TreeNode* Right *TreeNode* }*/// 解法一 合并排序func getAllElements(root1 *TreeNode, root2 *TreeNode) []int {arr1 := inorderTraversal(root1)arr2 := inorderTraversal(root2)arr1 = append(arr1, make([]int, len(arr2))...)merge(arr1, len(arr1)-len(arr2), arr2, len(arr2))return arr1}// this is 94 solutionfunc inorderTraversal(root *TreeNode) []int {var result []intinorder(root, &result)return result}func inorder(root *TreeNode, output *[]int) {if root != nil {inorder(root.Left, output)*output = append(*output, root.Val)inorder(root.Right, output)}}// this is 88 solutionfunc merge(nums1 []int, m int, nums2 []int, n int) {if m == 0 {copy(nums1, nums2)return}// 这里不需要,因为测试数据考虑到了第一个数组的空间问题// for index := 0; index < n; index++ {// nums1 = append(nums1, nums2[index])// }i := m - 1j := n - 1k := m + n - 1// 从后面往前放,只需要循环一次即可for ; i >= 0 && j >= 0; k-- {if nums1[i] > nums2[j] {nums1[k] = nums1[i]i--} else {nums1[k] = nums2[j]j--}}for ; j >= 0; k-- {nums1[k] = nums2[j]j--}}// 解法二 暴力遍历排序,时间复杂度高func getAllElements1(root1 *TreeNode, root2 *TreeNode) []int {arr := []int{}arr = append(arr, preorderTraversal(root1)...)arr = append(arr, preorderTraversal(root2)...)sort.Ints(arr)return arr}// this is 144 solutionfunc preorderTraversal(root *TreeNode) []int {res := []int{}if root != nil {res = append(res, root.Val)tmp := preorderTraversal(root.Left)for _, t := range tmp {res = append(res, t)}tmp = preorderTraversal(root.Right)for _, t := range tmp {res = append(res, t)}}return res}