mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 01:44:56 +08:00
update/0987: clean up redundant code
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
@ -23,37 +24,33 @@ type node struct {
|
||||
}
|
||||
|
||||
func verticalTraversal(root *TreeNode) [][]int {
|
||||
nodes := []*node{}
|
||||
inorder(root, 0, 0, &nodes)
|
||||
var dfs func(root *TreeNode, x, y int)
|
||||
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 {
|
||||
if nodes[i].y == nodes[j].y {
|
||||
if nodes[i].x < nodes[j].x {
|
||||
return true
|
||||
} else if nodes[i].x > nodes[j].x {
|
||||
return false
|
||||
}
|
||||
return nodes[i].val < nodes[j].val
|
||||
}
|
||||
return nodes[i].y < nodes[j].y
|
||||
a, b := nodes[i], nodes[j]
|
||||
return a.y < b.y || a.y == b.y &&
|
||||
(a.x < b.x || a.x == b.x && a.val < b.val)
|
||||
})
|
||||
res, currY, currColumn := [][]int{}, nodes[0].y, []int{nodes[0].val}
|
||||
for i := 1; i < len(nodes); i++ {
|
||||
if currY == nodes[i].y {
|
||||
currColumn = append(currColumn, nodes[i].val)
|
||||
|
||||
var res [][]int
|
||||
lastY := math.MinInt32
|
||||
for _, node := range nodes {
|
||||
if lastY != node.y {
|
||||
res = append(res, []int{node.val})
|
||||
lastY = node.y
|
||||
} else {
|
||||
res = append(res, currColumn)
|
||||
currColumn = []int{nodes[i].val}
|
||||
currY = nodes[i].y
|
||||
res[len(res)-1] = append(res[len(res)-1], node.val)
|
||||
}
|
||||
}
|
||||
res = append(res, currColumn)
|
||||
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
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
@ -101,38 +102,34 @@ type node struct {
|
||||
}
|
||||
|
||||
func verticalTraversal(root *TreeNode) [][]int {
|
||||
nodes := []*node{}
|
||||
inorder(root, 0, 0, &nodes)
|
||||
var dfs func(root *TreeNode, x, y int)
|
||||
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 {
|
||||
if nodes[i].y == nodes[j].y {
|
||||
if nodes[i].x < nodes[j].x {
|
||||
return true
|
||||
} else if nodes[i].x > nodes[j].x {
|
||||
return false
|
||||
}
|
||||
return nodes[i].val < nodes[j].val
|
||||
}
|
||||
return nodes[i].y < nodes[j].y
|
||||
a, b := nodes[i], nodes[j]
|
||||
return a.y < b.y || a.y == b.y &&
|
||||
(a.x < b.x || a.x == b.x && a.val < b.val)
|
||||
})
|
||||
res, currY, currColumn := [][]int{}, nodes[0].y, []int{nodes[0].val}
|
||||
for i := 1; i < len(nodes); i++ {
|
||||
if currY == nodes[i].y {
|
||||
currColumn = append(currColumn, nodes[i].val)
|
||||
|
||||
var res [][]int
|
||||
lastY := math.MinInt32
|
||||
for _, node := range nodes {
|
||||
if lastY != node.y {
|
||||
res = append(res, []int{node.val})
|
||||
lastY = node.y
|
||||
} else {
|
||||
res = append(res, currColumn)
|
||||
currColumn = []int{nodes[i].val}
|
||||
currY = nodes[i].y
|
||||
res[len(res)-1] = append(res[len(res)-1], node.val)
|
||||
}
|
||||
}
|
||||
res = append(res, currColumn)
|
||||
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
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
@ -101,40 +102,37 @@ type node struct {
|
||||
}
|
||||
|
||||
func verticalTraversal(root *TreeNode) [][]int {
|
||||
nodes := []*node{}
|
||||
inorder(root, 0, 0, &nodes)
|
||||
var dfs func(root *TreeNode, x, y int)
|
||||
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 {
|
||||
if nodes[i].y == nodes[j].y {
|
||||
if nodes[i].x < nodes[j].x {
|
||||
return true
|
||||
} else if nodes[i].x > nodes[j].x {
|
||||
return false
|
||||
}
|
||||
return nodes[i].val < nodes[j].val
|
||||
}
|
||||
return nodes[i].y < nodes[j].y
|
||||
a, b := nodes[i], nodes[j]
|
||||
return a.y < b.y || a.y == b.y &&
|
||||
(a.x < b.x || a.x == b.x && a.val < b.val)
|
||||
})
|
||||
res, currY, currColumn := [][]int{}, nodes[0].y, []int{nodes[0].val}
|
||||
for i := 1; i < len(nodes); i++ {
|
||||
if currY == nodes[i].y {
|
||||
currColumn = append(currColumn, nodes[i].val)
|
||||
|
||||
var res [][]int
|
||||
lastY := math.MinInt32
|
||||
for _, node := range nodes {
|
||||
if lastY != node.y {
|
||||
res = append(res, []int{node.val})
|
||||
lastY = node.y
|
||||
} else {
|
||||
res = append(res, currColumn)
|
||||
currColumn = []int{nodes[i].val}
|
||||
currY = nodes[i].y
|
||||
res[len(res)-1] = append(res[len(res)-1], node.val)
|
||||
}
|
||||
}
|
||||
res = append(res, currColumn)
|
||||
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