mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-12 22:55:19 +08:00
update/0987: clean up redundant code
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
package leetcode
|
package leetcode
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/halfrost/LeetCode-Go/structures"
|
"github.com/halfrost/LeetCode-Go/structures"
|
||||||
@ -23,37 +24,33 @@ type node struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func verticalTraversal(root *TreeNode) [][]int {
|
func verticalTraversal(root *TreeNode) [][]int {
|
||||||
nodes := []*node{}
|
var dfs func(root *TreeNode, x, y int)
|
||||||
inorder(root, 0, 0, &nodes)
|
var nodes []node
|
||||||
|
dfs = func(root *TreeNode, x, y int) {
|
||||||
|
if root == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
nodes = append(nodes, node{x, y, root.Val})
|
||||||
|
dfs(root.Left, x+1, y-1)
|
||||||
|
dfs(root.Right, x+1, y+1)
|
||||||
|
}
|
||||||
|
dfs(root, 0, 0)
|
||||||
|
|
||||||
sort.Slice(nodes, func(i, j int) bool {
|
sort.Slice(nodes, func(i, j int) bool {
|
||||||
if nodes[i].y == nodes[j].y {
|
a, b := nodes[i], nodes[j]
|
||||||
if nodes[i].x < nodes[j].x {
|
return a.y < b.y || a.y == b.y &&
|
||||||
return true
|
(a.x < b.x || a.x == b.x && a.val < b.val)
|
||||||
} else if nodes[i].x > nodes[j].x {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return nodes[i].val < nodes[j].val
|
|
||||||
}
|
|
||||||
return nodes[i].y < nodes[j].y
|
|
||||||
})
|
})
|
||||||
res, currY, currColumn := [][]int{}, nodes[0].y, []int{nodes[0].val}
|
|
||||||
for i := 1; i < len(nodes); i++ {
|
var res [][]int
|
||||||
if currY == nodes[i].y {
|
lastY := math.MinInt32
|
||||||
currColumn = append(currColumn, nodes[i].val)
|
for _, node := range nodes {
|
||||||
|
if lastY != node.y {
|
||||||
|
res = append(res, []int{node.val})
|
||||||
|
lastY = node.y
|
||||||
} else {
|
} else {
|
||||||
res = append(res, currColumn)
|
res[len(res)-1] = append(res[len(res)-1], node.val)
|
||||||
currColumn = []int{nodes[i].val}
|
|
||||||
currY = nodes[i].y
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res = append(res, currColumn)
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func inorder(root *TreeNode, x, y int, nodes *[]*node) {
|
|
||||||
if root != nil {
|
|
||||||
*nodes = append(*nodes, &node{x, y, root.Val})
|
|
||||||
inorder(root.Left, x+1, y-1, nodes)
|
|
||||||
inorder(root.Right, x+1, y+1, nodes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -79,6 +79,7 @@ Note that the solution remains the same since 5 and 6 are in the same location a
|
|||||||
package leetcode
|
package leetcode
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/halfrost/LeetCode-Go/structures"
|
"github.com/halfrost/LeetCode-Go/structures"
|
||||||
@ -101,38 +102,34 @@ type node struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func verticalTraversal(root *TreeNode) [][]int {
|
func verticalTraversal(root *TreeNode) [][]int {
|
||||||
nodes := []*node{}
|
var dfs func(root *TreeNode, x, y int)
|
||||||
inorder(root, 0, 0, &nodes)
|
var nodes []node
|
||||||
|
dfs = func(root *TreeNode, x, y int) {
|
||||||
|
if root == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
nodes = append(nodes, node{x, y, root.Val})
|
||||||
|
dfs(root.Left, x+1, y-1)
|
||||||
|
dfs(root.Right, x+1, y+1)
|
||||||
|
}
|
||||||
|
dfs(root, 0, 0)
|
||||||
|
|
||||||
sort.Slice(nodes, func(i, j int) bool {
|
sort.Slice(nodes, func(i, j int) bool {
|
||||||
if nodes[i].y == nodes[j].y {
|
a, b := nodes[i], nodes[j]
|
||||||
if nodes[i].x < nodes[j].x {
|
return a.y < b.y || a.y == b.y &&
|
||||||
return true
|
(a.x < b.x || a.x == b.x && a.val < b.val)
|
||||||
} else if nodes[i].x > nodes[j].x {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return nodes[i].val < nodes[j].val
|
|
||||||
}
|
|
||||||
return nodes[i].y < nodes[j].y
|
|
||||||
})
|
})
|
||||||
res, currY, currColumn := [][]int{}, nodes[0].y, []int{nodes[0].val}
|
|
||||||
for i := 1; i < len(nodes); i++ {
|
var res [][]int
|
||||||
if currY == nodes[i].y {
|
lastY := math.MinInt32
|
||||||
currColumn = append(currColumn, nodes[i].val)
|
for _, node := range nodes {
|
||||||
|
if lastY != node.y {
|
||||||
|
res = append(res, []int{node.val})
|
||||||
|
lastY = node.y
|
||||||
} else {
|
} else {
|
||||||
res = append(res, currColumn)
|
res[len(res)-1] = append(res[len(res)-1], node.val)
|
||||||
currColumn = []int{nodes[i].val}
|
|
||||||
currY = nodes[i].y
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res = append(res, currColumn)
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func inorder(root *TreeNode, x, y int, nodes *[]*node) {
|
|
||||||
if root != nil {
|
|
||||||
*nodes = append(*nodes, &node{x, y, root.Val})
|
|
||||||
inorder(root.Left, x+1, y-1, nodes)
|
|
||||||
inorder(root.Right, x+1, y+1, nodes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
@ -79,6 +79,7 @@ Note that the solution remains the same since 5 and 6 are in the same location a
|
|||||||
package leetcode
|
package leetcode
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/halfrost/LeetCode-Go/structures"
|
"github.com/halfrost/LeetCode-Go/structures"
|
||||||
@ -101,40 +102,37 @@ type node struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func verticalTraversal(root *TreeNode) [][]int {
|
func verticalTraversal(root *TreeNode) [][]int {
|
||||||
nodes := []*node{}
|
var dfs func(root *TreeNode, x, y int)
|
||||||
inorder(root, 0, 0, &nodes)
|
var nodes []node
|
||||||
|
dfs = func(root *TreeNode, x, y int) {
|
||||||
|
if root == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
nodes = append(nodes, node{x, y, root.Val})
|
||||||
|
dfs(root.Left, x+1, y-1)
|
||||||
|
dfs(root.Right, x+1, y+1)
|
||||||
|
}
|
||||||
|
dfs(root, 0, 0)
|
||||||
|
|
||||||
sort.Slice(nodes, func(i, j int) bool {
|
sort.Slice(nodes, func(i, j int) bool {
|
||||||
if nodes[i].y == nodes[j].y {
|
a, b := nodes[i], nodes[j]
|
||||||
if nodes[i].x < nodes[j].x {
|
return a.y < b.y || a.y == b.y &&
|
||||||
return true
|
(a.x < b.x || a.x == b.x && a.val < b.val)
|
||||||
} else if nodes[i].x > nodes[j].x {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return nodes[i].val < nodes[j].val
|
|
||||||
}
|
|
||||||
return nodes[i].y < nodes[j].y
|
|
||||||
})
|
})
|
||||||
res, currY, currColumn := [][]int{}, nodes[0].y, []int{nodes[0].val}
|
|
||||||
for i := 1; i < len(nodes); i++ {
|
var res [][]int
|
||||||
if currY == nodes[i].y {
|
lastY := math.MinInt32
|
||||||
currColumn = append(currColumn, nodes[i].val)
|
for _, node := range nodes {
|
||||||
|
if lastY != node.y {
|
||||||
|
res = append(res, []int{node.val})
|
||||||
|
lastY = node.y
|
||||||
} else {
|
} else {
|
||||||
res = append(res, currColumn)
|
res[len(res)-1] = append(res[len(res)-1], node.val)
|
||||||
currColumn = []int{nodes[i].val}
|
|
||||||
currY = nodes[i].y
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res = append(res, currColumn)
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func inorder(root *TreeNode, x, y int, nodes *[]*node) {
|
|
||||||
if root != nil {
|
|
||||||
*nodes = append(*nodes, &node{x, y, root.Val})
|
|
||||||
inorder(root.Left, x+1, y-1, nodes)
|
|
||||||
inorder(root.Right, x+1, y+1, nodes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user