From 8e44f0a11b1f31c03b8cbcc1c3cda4d37042d1ff Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sat, 12 Jun 2021 19:56:09 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20404.=E5=B7=A6=E5=8F=B6?= =?UTF-8?q?=E5=AD=90=E4=B9=8B=E5=92=8C=20Javascript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 404.左叶子之和 Javascript版本 --- problems/0404.左叶子之和.md | 65 ++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index e5daa9db..e6a6682b 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -226,6 +226,71 @@ class Solution: ``` Go: +> 递归法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func sumOfLeftLeaves(root *TreeNode) int { + var res int + findLeft(root,&res) + return res +} +func findLeft(root *TreeNode,res *int){ + //左节点 + if root.Left!=nil&&root.Left.Left==nil&&root.Left.Right==nil{ + *res=*res+root.Left.Val + } + if root.Left!=nil{ + findLeft(root.Left,res) + } + if root.Right!=nil{ + findLeft(root.Right,res) + } +} +``` + +> 迭代法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func sumOfLeftLeaves(root *TreeNode) int { + var res int + queue:=list.New() + queue.PushBack(root) + for queue.Len()>0{ + length:=queue.Len() + for i:=0;i Date: Sat, 12 Jun 2021 21:55:15 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20513.=E6=89=BE=E6=A0=91?= =?UTF-8?q?=E5=B7=A6=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC=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 添加 513.找树左下角的值 go版本 --- problems/0513.找树左下角的值.md | 74 ++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index 97464e3d..e252ccd3 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -298,6 +298,80 @@ class Solution: ``` Go: +> 递归法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + var maxDeep int // 全局变量 深度 + var value int //全局变量 最终值 +func findBottomLeftValue(root *TreeNode) int { + if root.Left==nil&&root.Right==nil{//需要提前判断一下(不要这个if的话提交结果会出错,但执行代码不会。防止这种情况出现,故先判断是否只有一个节点) + return root.Val + } + findLeftValue (root,maxDeep) + return value +} +func findLeftValue (root *TreeNode,deep int){ + //最左边的值在左边 + if root.Left==nil&&root.Right==nil{ + if deep>maxDeep{ + value=root.Val + maxDeep=deep + } + } + //递归 + if root.Left!=nil{ + deep++ + findLeftValue(root.Left,deep) + deep--//回溯 + } + if root.Right!=nil{ + deep++ + findLeftValue(root.Right,deep) + deep--//回溯 + } +} +``` + +> 迭代法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func findBottomLeftValue(root *TreeNode) int { + queue:=list.New() + var gradation int + queue.PushBack(root) + for queue.Len()>0{ + length:=queue.Len() + for i:=0;i Date: Sat, 12 Jun 2021 21:57:47 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20404.=E5=B7=A6=E5=8F=B6?= =?UTF-8?q?=E5=AD=90=E4=B9=8B=E5=92=8C=20GO=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 404.左叶子之和 GO版本 --- problems/0404.左叶子之和.md | 1 + 1 file changed, 1 insertion(+) diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index e6a6682b..aa758367 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -340,6 +340,7 @@ var sumOfLeftLeaves = function(root) { + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From 93b8dda7ca2567d11198c2dd04df48864f39d645 Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sat, 12 Jun 2021 23:34:37 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200112.=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=80=BB=E5=92=8C=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0112.路径总和.md | 86 +++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index d810a046..54f79d1d 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -486,6 +486,92 @@ class Solution: Go: +> 112. 路径总和 + +```go +//递归法 +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func hasPathSum(root *TreeNode, targetSum int) bool { + var flage bool //找没找到的标志 + if root==nil{ + return flage + } + pathSum(root,0,targetSum,&flage) + return flage +} +func pathSum(root *TreeNode, sum int,targetSum int,flage *bool){ + sum+=root.Val + if root.Left==nil&&root.Right==nil&&sum==targetSum{ + *flage=true + return + } + if root.Left!=nil&&!(*flage){//左节点不为空且还没找到 + pathSum(root.Left,sum,targetSum,flage) + } + if root.Right!=nil&&!(*flage){//右节点不为空且没找到 + pathSum(root.Right,sum,targetSum,flage) + } +} +``` + + + +> 113 递归法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func pathSum(root *TreeNode, targetSum int) [][]int { + var result [][]int//最终结果 + if root==nil{ + return result + } + var sumNodes []int//经过路径的节点集合 + hasPathSum(root,&sumNodes,targetSum,&result) + return result +} +func hasPathSum(root *TreeNode,sumNodes *[]int,targetSum int,result *[][]int){ + *sumNodes=append(*sumNodes,root.Val) + if root.Left==nil&&root.Right==nil{//叶子节点 + fmt.Println(*sumNodes) + var sum int + var number int + for k,v:=range *sumNodes{//求该路径节点的和 + sum+=v + number=k + } + tempNodes:=make([]int,number+1)//新的nodes接受指针里的值,防止最终指针里的值发生变动,导致最后的结果都是最后一个sumNodes的值 + for k,v:=range *sumNodes{ + tempNodes[k]=v + } + if sum==targetSum{ + *result=append(*result,tempNodes) + } + } + if root.Left!=nil{ + hasPathSum(root.Left,sumNodes,targetSum,result) + *sumNodes=(*sumNodes)[:len(*sumNodes)-1]//回溯 + } + if root.Right!=nil{ + hasPathSum(root.Right,sumNodes,targetSum,result) + *sumNodes=(*sumNodes)[:len(*sumNodes)-1]//回溯 + } +} +``` + JavaScript: 0112.路径总和