From fa00cd53b597d9ce837319f1967d0ae6478c430e Mon Sep 17 00:00:00 2001 From: lingyin Date: Wed, 6 Apr 2022 10:28:57 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=AE=80=E5=8C=96Peek=E5=87=BD=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E5=A4=8D=E7=94=A8Pop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0232.用栈实现队列.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index b00ffd80..1a56d9f3 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -275,15 +275,11 @@ func (this *MyQueue) Pop() int { /** Get the front element. */ func (this *MyQueue) Peek() int { - for len(this.stack) != 0 { - val := this.stack[len(this.stack)-1] - this.stack = this.stack[:len(this.stack)-1] - this.back = append(this.back, val) - } - if len(this.back) == 0 { + val := this.Pop() + if val == 0 { return 0 } - val := this.back[len(this.back)-1] + this.back = append(this.back, val) return val } From 888bd8fe0b2320ffd0afe4cfe014e79116d23944 Mon Sep 17 00:00:00 2001 From: dmzlingyin Date: Sat, 9 Apr 2022 00:12:46 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(ACM=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E5=A6=82=E4=BD=95=E6=9E=84=E5=BB=BA=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?.md):=20Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../前序/ACM模式如何构建二叉树.md | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/problems/前序/ACM模式如何构建二叉树.md b/problems/前序/ACM模式如何构建二叉树.md index bd2e9780..dba87ca1 100644 --- a/problems/前序/ACM模式如何构建二叉树.md +++ b/problems/前序/ACM模式如何构建二叉树.md @@ -220,6 +220,72 @@ int main() { ## Go ```Go +package main + +import "fmt" + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +func constructBinaryTree(array []int) *TreeNode { + var root *TreeNode + nodes := make([]*TreeNode, len(array)) + + // 初始化二叉树节点 + for i := 0; i < len(nodes); i++ { + var node *TreeNode + if array[i] != -1 { + node = &TreeNode{Val: array[i]} + } + nodes[i] = node + if i == 0 { + root = node + } + } + // 串联节点 + for i := 0; i*2+2 < len(array); i++ { + if nodes[i] != nil { + nodes[i].Left = nodes[i*2+1] + nodes[i].Right = nodes[i*2+2] + } + } + return root +} + +func printBinaryTree(root *TreeNode, n int) { + var queue []*TreeNode + if root != nil { + queue = append(queue, root) + } + + result := []int{} + for len(queue) > 0 { + for j := 0; j < len(queue); j++ { + node := queue[j] + if node != nil { + result = append(result, node.Val) + queue = append(queue, node.Left) + queue = append(queue, node.Right) + } else { + result = append(result, -1) + } + } + // 清除队列中的本层节点, 进入下一层遍历 + queue = queue[len(queue):] + } + // 参数n控制输出值数量, 否则二叉树最后一层叶子节点的孩子节点也会被打印(但是这些孩子节点是不存在的). + fmt.Println(result[:n]) +} + +func main() { + array := []int{4, 1, 6, 0, 2, 5, 7, -1, -1, -1, 3, -1, -1, -1, 8} + root := constructBinaryTree(array) + printBinaryTree(root, len(array)) +} + ``` ## JavaScript From 7ad56a33e0cb2313fd0bdaaaf1bccb26f6b09b1f Mon Sep 17 00:00:00 2001 From: lingyin <32977462+dmzlingyin@users.noreply.github.com> Date: Sat, 9 Apr 2022 00:19:52 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Update=20ACM=E6=A8=A1=E5=BC=8F=E5=A6=82?= =?UTF-8?q?=E4=BD=95=E6=9E=84=E5=BB=BA=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../前序/ACM模式如何构建二叉树.md | 93 ++++++++++--------- 1 file changed, 47 insertions(+), 46 deletions(-) diff --git a/problems/前序/ACM模式如何构建二叉树.md b/problems/前序/ACM模式如何构建二叉树.md index dba87ca1..444f0071 100644 --- a/problems/前序/ACM模式如何构建二叉树.md +++ b/problems/前序/ACM模式如何构建二叉树.md @@ -225,65 +225,66 @@ package main import "fmt" type TreeNode struct { - Val int - Left *TreeNode - Right *TreeNode + Val int + Left *TreeNode + Right *TreeNode } func constructBinaryTree(array []int) *TreeNode { - var root *TreeNode - nodes := make([]*TreeNode, len(array)) + var root *TreeNode + nodes := make([]*TreeNode, len(array)) - // 初始化二叉树节点 - for i := 0; i < len(nodes); i++ { - var node *TreeNode - if array[i] != -1 { - node = &TreeNode{Val: array[i]} - } - nodes[i] = node - if i == 0 { - root = node - } + // 初始化二叉树节点 + for i := 0; i < len(nodes); i++ { + var node *TreeNode + if array[i] != -1 { + node = &TreeNode{Val: array[i]} } - // 串联节点 - for i := 0; i*2+2 < len(array); i++ { - if nodes[i] != nil { - nodes[i].Left = nodes[i*2+1] - nodes[i].Right = nodes[i*2+2] - } + nodes[i] = node + if i == 0 { + root = node } - return root + } + // 串联节点 + for i := 0; i*2+2 < len(array); i++ { + if nodes[i] != nil { + nodes[i].Left = nodes[i*2+1] + nodes[i].Right = nodes[i*2+2] + } + } + return root } func printBinaryTree(root *TreeNode, n int) { - var queue []*TreeNode - if root != nil { - queue = append(queue, root) - } + var queue []*TreeNode + if root != nil { + queue = append(queue, root) + } - result := []int{} - for len(queue) > 0 { - for j := 0; j < len(queue); j++ { - node := queue[j] - if node != nil { - result = append(result, node.Val) - queue = append(queue, node.Left) - queue = append(queue, node.Right) - } else { - result = append(result, -1) - } - } - // 清除队列中的本层节点, 进入下一层遍历 - queue = queue[len(queue):] - } - // 参数n控制输出值数量, 否则二叉树最后一层叶子节点的孩子节点也会被打印(但是这些孩子节点是不存在的). - fmt.Println(result[:n]) + result := []int{} + for len(queue) > 0 { + for j := 0; j < len(queue); j++ { + node := queue[j] + if node != nil { + result = append(result, node.Val) + queue = append(queue, node.Left) + queue = append(queue, node.Right) + } else { + result = append(result, -1) + } + } + // 清除队列中的本层节点, 进入下一层遍历 + queue = queue[len(queue):] + } + + // 参数n控制输出值数量, 否则二叉树最后一层叶子节点的孩子节点也会被打印(但是这些孩子节点是不存在的). + fmt.Println(result[:n]) } func main() { - array := []int{4, 1, 6, 0, 2, 5, 7, -1, -1, -1, 3, -1, -1, -1, 8} - root := constructBinaryTree(array) - printBinaryTree(root, len(array)) + array := []int{4, 1, 6, 0, 2, 5, 7, -1, -1, -1, 3, -1, -1, -1, 8} + root := constructBinaryTree(array) + printBinaryTree(root, len(array)) } ```