Skip to content

Commit e445874

Browse files
committed
update/0987: clean up redundant code
1 parent 2608238 commit e445874

File tree

3 files changed

+72
-80
lines changed

3 files changed

+72
-80
lines changed
Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package leetcode
22

33
import (
4+
"math"
45
"sort"
56

67
"github.com/halfrost/LeetCode-Go/structures"
@@ -23,37 +24,33 @@ type node struct {
2324
}
2425

2526
func verticalTraversal(root *TreeNode) [][]int {
26-
nodes := []*node{}
27-
inorder(root, 0, 0, &nodes)
28-
sort.Slice(nodes, func(i, j int) bool {
29-
if nodes[i].y == nodes[j].y {
30-
if nodes[i].x < nodes[j].x {
31-
return true
32-
} else if nodes[i].x > nodes[j].x {
33-
return false
34-
}
35-
return nodes[i].val < nodes[j].val
27+
var dfs func(root *TreeNode, x, y int)
28+
var nodes []node
29+
dfs = func(root *TreeNode, x, y int) {
30+
if root == nil {
31+
return
3632
}
37-
return nodes[i].y < nodes[j].y
33+
nodes = append(nodes, node{x, y, root.Val})
34+
dfs(root.Left, x+1, y-1)
35+
dfs(root.Right, x+1, y+1)
36+
}
37+
dfs(root, 0, 0)
38+
39+
sort.Slice(nodes, func(i, j int) bool {
40+
a, b := nodes[i], nodes[j]
41+
return a.y < b.y || a.y == b.y &&
42+
(a.x < b.x || a.x == b.x && a.val < b.val)
3843
})
39-
res, currY, currColumn := [][]int{}, nodes[0].y, []int{nodes[0].val}
40-
for i := 1; i < len(nodes); i++ {
41-
if currY == nodes[i].y {
42-
currColumn = append(currColumn, nodes[i].val)
44+
45+
var res [][]int
46+
lastY := math.MinInt32
47+
for _, node := range nodes {
48+
if lastY != node.y {
49+
res = append(res, []int{node.val})
50+
lastY = node.y
4351
} else {
44-
res = append(res, currColumn)
45-
currColumn = []int{nodes[i].val}
46-
currY = nodes[i].y
52+
res[len(res)-1] = append(res[len(res)-1], node.val)
4753
}
4854
}
49-
res = append(res, currColumn)
5055
return res
5156
}
52-
53-
func inorder(root *TreeNode, x, y int, nodes *[]*node) {
54-
if root != nil {
55-
*nodes = append(*nodes, &node{x, y, root.Val})
56-
inorder(root.Left, x+1, y-1, nodes)
57-
inorder(root.Right, x+1, y+1, nodes)
58-
}
59-
}

leetcode/0987.Vertical-Order-Traversal-of-a-Binary-Tree/README.md

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Note that the solution remains the same since 5 and 6 are in the same location a
7979
package leetcode
8080

8181
import (
82+
"math"
8283
"sort"
8384

8485
"github.com/halfrost/LeetCode-Go/structures"
@@ -101,38 +102,34 @@ type node struct {
101102
}
102103

103104
func verticalTraversal(root *TreeNode) [][]int {
104-
nodes := []*node{}
105-
inorder(root, 0, 0, &nodes)
106-
sort.Slice(nodes, func(i, j int) bool {
107-
if nodes[i].y == nodes[j].y {
108-
if nodes[i].x < nodes[j].x {
109-
return true
110-
} else if nodes[i].x > nodes[j].x {
111-
return false
112-
}
113-
return nodes[i].val < nodes[j].val
105+
var dfs func(root *TreeNode, x, y int)
106+
var nodes []node
107+
dfs = func(root *TreeNode, x, y int) {
108+
if root == nil {
109+
return
114110
}
115-
return nodes[i].y < nodes[j].y
111+
nodes = append(nodes, node{x, y, root.Val})
112+
dfs(root.Left, x+1, y-1)
113+
dfs(root.Right, x+1, y+1)
114+
}
115+
dfs(root, 0, 0)
116+
117+
sort.Slice(nodes, func(i, j int) bool {
118+
a, b := nodes[i], nodes[j]
119+
return a.y < b.y || a.y == b.y &&
120+
(a.x < b.x || a.x == b.x && a.val < b.val)
116121
})
117-
res, currY, currColumn := [][]int{}, nodes[0].y, []int{nodes[0].val}
118-
for i := 1; i < len(nodes); i++ {
119-
if currY == nodes[i].y {
120-
currColumn = append(currColumn, nodes[i].val)
122+
123+
var res [][]int
124+
lastY := math.MinInt32
125+
for _, node := range nodes {
126+
if lastY != node.y {
127+
res = append(res, []int{node.val})
128+
lastY = node.y
121129
} else {
122-
res = append(res, currColumn)
123-
currColumn = []int{nodes[i].val}
124-
currY = nodes[i].y
130+
res[len(res)-1] = append(res[len(res)-1], node.val)
125131
}
126132
}
127-
res = append(res, currColumn)
128133
return res
129134
}
130-
131-
func inorder(root *TreeNode, x, y int, nodes *[]*node) {
132-
if root != nil {
133-
*nodes = append(*nodes, &node{x, y, root.Val})
134-
inorder(root.Left, x+1, y-1, nodes)
135-
inorder(root.Right, x+1, y+1, nodes)
136-
}
137-
}
138135
```

website/content/ChapterFour/0900~0999/0987.Vertical-Order-Traversal-of-a-Binary-Tree.md

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Note that the solution remains the same since 5 and 6 are in the same location a
7979
package leetcode
8080

8181
import (
82+
"math"
8283
"sort"
8384

8485
"github.com/halfrost/LeetCode-Go/structures"
@@ -101,40 +102,37 @@ type node struct {
101102
}
102103

103104
func verticalTraversal(root *TreeNode) [][]int {
104-
nodes := []*node{}
105-
inorder(root, 0, 0, &nodes)
106-
sort.Slice(nodes, func(i, j int) bool {
107-
if nodes[i].y == nodes[j].y {
108-
if nodes[i].x < nodes[j].x {
109-
return true
110-
} else if nodes[i].x > nodes[j].x {
111-
return false
112-
}
113-
return nodes[i].val < nodes[j].val
105+
var dfs func(root *TreeNode, x, y int)
106+
var nodes []node
107+
dfs = func(root *TreeNode, x, y int) {
108+
if root == nil {
109+
return
114110
}
115-
return nodes[i].y < nodes[j].y
111+
nodes = append(nodes, node{x, y, root.Val})
112+
dfs(root.Left, x+1, y-1)
113+
dfs(root.Right, x+1, y+1)
114+
}
115+
dfs(root, 0, 0)
116+
117+
sort.Slice(nodes, func(i, j int) bool {
118+
a, b := nodes[i], nodes[j]
119+
return a.y < b.y || a.y == b.y &&
120+
(a.x < b.x || a.x == b.x && a.val < b.val)
116121
})
117-
res, currY, currColumn := [][]int{}, nodes[0].y, []int{nodes[0].val}
118-
for i := 1; i < len(nodes); i++ {
119-
if currY == nodes[i].y {
120-
currColumn = append(currColumn, nodes[i].val)
122+
123+
var res [][]int
124+
lastY := math.MinInt32
125+
for _, node := range nodes {
126+
if lastY != node.y {
127+
res = append(res, []int{node.val})
128+
lastY = node.y
121129
} else {
122-
res = append(res, currColumn)
123-
currColumn = []int{nodes[i].val}
124-
currY = nodes[i].y
130+
res[len(res)-1] = append(res[len(res)-1], node.val)
125131
}
126132
}
127-
res = append(res, currColumn)
128133
return res
129134
}
130135

131-
func inorder(root *TreeNode, x, y int, nodes *[]*node) {
132-
if root != nil {
133-
*nodes = append(*nodes, &node{x, y, root.Val})
134-
inorder(root.Left, x+1, y-1, nodes)
135-
inorder(root.Right, x+1, y+1, nodes)
136-
}
137-
}
138136
```
139137

140138

0 commit comments

Comments
 (0)