From 8df256e8dc58812274009ef5fd8bad90d31a8f21 Mon Sep 17 00:00:00 2001 From: baici1 <249337001@qq.com> Date: Wed, 8 Sep 2021 19:18:02 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0104=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6=E7=9A=84?= =?UTF-8?q?go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 36 ++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index ac43f0a5..598e274f 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1570,9 +1570,43 @@ class Solution: return len(result) ``` - Go: +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func maxDepth(root *TreeNode) int { + ans:=0 + if root==nil{ + return 0 + } + queue:=list.New() + queue.PushBack(root) + for queue.Len()>0{ + length:=queue.Len() + for i:=0;i Date: Wed, 8 Sep 2021 19:20:34 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0111.=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6=20go?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 598e274f..a2fd6f03 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1708,6 +1708,46 @@ class Solution: Go: +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func minDepth(root *TreeNode) int { + ans:=0 + if root==nil{ + return 0 + } + queue:=list.New() + queue.PushBack(root) + for queue.Len()>0{ + length:=queue.Len() + for i:=0;i Date: Wed, 8 Sep 2021 22:17:37 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200416.=E5=88=86?= =?UTF-8?q?=E5=89=B2=E7=AD=89=E5=92=8C=E5=AD=90=E9=9B=86=20go=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0416.分割等和子集 go版本 --- problems/0416.分割等和子集.md | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index 599a3180..415ff88b 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -227,6 +227,45 @@ class Solution: ``` Go: +``` +func canPartition(nums []int) bool { + /** + 动态五部曲: + 1.确定dp数组和下标含义 + 2.确定递推公式 + 3.dp数组初始化 + 4.dp遍历顺序 + 5.打印 + **/ + //确定和 + var sum int + for _,v:=range nums{ + sum+=v + } + if sum%2!=0{ //如果和为奇数,则不可能分成两个相等的数组 + return false + } + sum/=2 + //确定dp数组和下标含义 + var dp [][]bool //dp[i][j] 表示: 前i个石头是否总和不大于J + //初始化数组 + dp=make([][]bool,len(nums)+1) + for i,_:=range dp{ + dp[i]=make([]bool,sum+1) + dp[i][0]=true + } + for i:=1;i<=len(nums);i++{ + for j:=1;j<=sum;j++{//j是固定总量 + if j>=nums[i-1]{//如果容量够用则可放入背包 + dp[i][j]=dp[i-1][j]||dp[i-1][j-nums[i-1]] + }else{//如果容量不够用则不拿,维持前一个状态 + dp[i][j]=dp[i-1][j] + } + } + } + return dp[len(nums)][sum] +} +``` javaScript: From 09013df96a8263dda2ed601acc04e69107ff2b46 Mon Sep 17 00:00:00 2001 From: baici1 <249337001@qq.com> Date: Thu, 9 Sep 2021 15:52:45 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0226=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E5=AD=90=E6=A0=91=20go=E7=89=88=E6=9C=AC=E7=9A=84=E9=80=92?= =?UTF-8?q?=E5=BD=92=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=EF=BC=8C=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E5=89=8D=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=EF=BC=8C?= =?UTF-8?q?=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0226.翻转二叉树.md | 94 +++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index b69cad55..fb5d831a 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -363,7 +363,9 @@ class Solution: return root ``` -### Go +### Go + +递归版本的前序遍历 ```Go func invertTree(root *TreeNode) *TreeNode { @@ -381,6 +383,96 @@ func invertTree(root *TreeNode) *TreeNode { } ``` +递归版本的后序遍历 + +```go +func invertTree(root *TreeNode) *TreeNode { + if root==nil{ + return root + } + invertTree(root.Left)//遍历左节点 + invertTree(root.Right)//遍历右节点 + root.Left,root.Right=root.Right,root.Left//交换 + return root +} +``` + +迭代版本的前序遍历 + +```go +func invertTree(root *TreeNode) *TreeNode { + stack:=[]*TreeNode{} + node:=root + for node!=nil||len(stack)>0{ + for node!=nil{ + node.Left,node.Right=node.Right,node.Left//交换 + stack=append(stack,node) + node=node.Left + } + node=stack[len(stack)-1] + stack=stack[:len(stack)-1] + node=node.Right + } + return root +} +``` + +迭代版本的后序遍历 + +```go +func invertTree(root *TreeNode) *TreeNode { + stack:=[]*TreeNode{} + node:=root + var prev *TreeNode + for node!=nil||len(stack)>0{ + for node!=nil{ + stack=append(stack,node) + node=node.Left + } + node=stack[len(stack)-1] + stack=stack[:len(stack)-1] + if node.Right==nil||node.Right==prev{ + node.Left,node.Right=node.Right,node.Left//交换 + prev=node + node=nil + }else { + stack=append(stack,node) + node=node.Right + } + } + return root +} +``` + +层序遍历 + +```go +func invertTree(root *TreeNode) *TreeNode { + if root==nil{ + return root + } + queue:=list.New() + node:=root + queue.PushBack(node) + for queue.Len()>0{ + length:=queue.Len() + for i:=0;i