diff --git a/leetcode/0987.Vertical-Order-Traversal-of-a-Binary-Tree/987. Vertical Order Traversal of a Binary Tree.go b/leetcode/0987.Vertical-Order-Traversal-of-a-Binary-Tree/987. Vertical Order Traversal of a Binary Tree.go index 2e49453d..a7937eb4 100644 --- a/leetcode/0987.Vertical-Order-Traversal-of-a-Binary-Tree/987. Vertical Order Traversal of a Binary Tree.go +++ b/leetcode/0987.Vertical-Order-Traversal-of-a-Binary-Tree/987. Vertical Order Traversal of a Binary Tree.go @@ -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) - 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 + var dfs func(root *TreeNode, x, y int) + var nodes []node + dfs = func(root *TreeNode, x, y int) { + if root == nil { + return } - return nodes[i].y < nodes[j].y + 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 { + 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) - } -} diff --git a/leetcode/0987.Vertical-Order-Traversal-of-a-Binary-Tree/README.md b/leetcode/0987.Vertical-Order-Traversal-of-a-Binary-Tree/README.md index 3a72b783..daa44efe 100644 --- a/leetcode/0987.Vertical-Order-Traversal-of-a-Binary-Tree/README.md +++ b/leetcode/0987.Vertical-Order-Traversal-of-a-Binary-Tree/README.md @@ -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) - 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 + var dfs func(root *TreeNode, x, y int) + var nodes []node + dfs = func(root *TreeNode, x, y int) { + if root == nil { + return } - return nodes[i].y < nodes[j].y + 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 { + 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) - } -} ``` \ No newline at end of file diff --git a/website/content/ChapterFour/0900~0999/0987.Vertical-Order-Traversal-of-a-Binary-Tree.md b/website/content/ChapterFour/0900~0999/0987.Vertical-Order-Traversal-of-a-Binary-Tree.md index 9f6cd932..c0938172 100644 --- a/website/content/ChapterFour/0900~0999/0987.Vertical-Order-Traversal-of-a-Binary-Tree.md +++ b/website/content/ChapterFour/0900~0999/0987.Vertical-Order-Traversal-of-a-Binary-Tree.md @@ -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) - 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 + var dfs func(root *TreeNode, x, y int) + var nodes []node + dfs = func(root *TreeNode, x, y int) { + if root == nil { + return } - return nodes[i].y < nodes[j].y + 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 { + 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) - } -} ```