From dc03975acdb47f6c5eaa0b2b3a47d119f8fe78f2 Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Tue, 29 Jun 2021 00:20:13 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=200098.=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=20go?= =?UTF-8?q?=E7=89=88=20=EF=BC=88=E5=A2=9E=E5=8A=A0=E4=B8=AD=E5=BA=8F?= =?UTF-8?q?=E9=81=8D=E5=8E=86=E8=A7=A3=E9=A2=98=E6=B3=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加中序遍历解题法 --- ...11\346\220\234\347\264\242\346\240\221.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index e38c5adea7..b93d8cd5fd 100644 --- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -376,6 +376,28 @@ func isBST(root *TreeNode, min, max int) bool { return isBST(root.Left, min, root.Val) && isBST(root.Right, root.Val, max) } ``` +```go +// 中序遍历解法 +func isValidBST(root *TreeNode) bool { + // 保存上一个指针 + var prev *TreeNode + var travel func(node *TreeNode) bool + travel = func(node *TreeNode) bool { + if node == nil { + return true + } + leftRes := travel(node.Left) + // 当前值小于等于前一个节点的值,返回false + if prev != nil && node.Val <= prev.Val { + return false + } + prev = node + rightRes := travel(node.Right) + return leftRes && rightRes + } + return travel(root) +} +``` JavaScript版本 From 011b0cc6fdd8058b738984066f075b37eb8245fc Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Tue, 29 Jun 2021 21:55:02 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=200530.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F?= =?UTF-8?q?=E7=BB=9D=E5=AF=B9=E5=B7=AE=20go=E7=89=88=20=EF=BC=88=E4=B8=AD?= =?UTF-8?q?=E5=BA=8F=E9=81=8D=E5=8E=86=E7=9A=84=E5=90=8C=E6=97=B6=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E6=9C=80=E5=B0=8F=E5=80=BC=EF=BC=8C=E4=B8=8D=E7=94=A8?= =?UTF-8?q?=E9=A2=9D=E5=A4=96=E7=9A=84=E5=AD=98=E5=82=A8=E7=A9=BA=E9=97=B4?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 中序遍历的同时计算最小值,不用额外的存储空间 --- ...17\347\273\235\345\257\271\345\267\256.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" index 8fa756bc80..47b2b4348b 100644 --- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" +++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" @@ -255,6 +255,29 @@ func findMIn(root *TreeNode,res *[]int){ findMIn(root.Right,res) } ``` +```go +// 中序遍历的同时计算最小值 +func getMinimumDifference(root *TreeNode) int { + // 保留前一个节点的指针 + var prev *TreeNode + // 定义一个比较大的值 + min := math.MaxInt64 + var travel func(node *TreeNode) + travel = func(node *TreeNode) { + if node == nil { + return + } + travel(node.Left) + if prev != nil && node.Val - prev.Val < min { + min = node.Val - prev.Val + } + prev = node + travel(node.Right) + } + travel(root) + return min +} +``` JavaScript版本 From f85d1c1a3792d848f42f7fda03fbab5fd712d29c Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Tue, 29 Jun 2021 22:41:40 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200501.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97?= =?UTF-8?q?=E6=95=B0=20go=E7=89=88=EF=BC=88=E5=8E=9F=E5=85=88=E7=9A=84?= =?UTF-8?q?=E5=86=99=E6=B3=95=E6=9C=89=E9=97=AE=E9=A2=98=EF=BC=8C=E8=BF=87?= =?UTF-8?q?=E4=B8=8D=E4=BA=86leetcode=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原先的写法有问题,过不了leetcode,更新正确的解法 --- ...55\347\232\204\344\274\227\346\225\260.md" | 64 +++++++++---------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" index f4b239c386..d2326d6703 100644 --- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" +++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" @@ -474,7 +474,7 @@ func traversal(root *TreeNode,history map[int]int){ } ``` -计数法BSL(此代码在执行代码里能执行,但提交后报错,不知为何,思路是对的) +计数法,不使用额外空间,利用二叉树性质,中序遍历 ```go /** @@ -485,41 +485,35 @@ func traversal(root *TreeNode,history map[int]int){ * Right *TreeNode * } */ - var count,maxCount int //统计计数 -func findMode(root *TreeNode) []int { - var result []int - var pre *TreeNode //前指针 - if root.Left==nil&&root.Right==nil{ - result=append(result,root.Val) - return result - } - traversal(root,&result,pre) - return result -} -func traversal(root *TreeNode,result *[]int,pre *TreeNode){//遍历统计 - //如果BSL中序遍历相邻的两个节点值相同,则统计频率;如果不相同,依据BSL中序遍历排好序的性质,重新计数 - if pre==nil{ - count=1 - }else if pre.Val==root.Val{ - count++ - }else { - count=1 - } - //如果统计的频率等于最大频率,则加入结果集;如果统计的频率大于最大频率,更新最大频率且重新将结果加入新的结果集中 - if count==maxCount{ - *result=append(*result,root.Val) - }else if count>maxCount{ - maxCount=count//重新赋值maxCount - *result=[]int{}//清空result中的内容 - *result=append(*result,root.Val) - } - pre=root//保存上一个的节点 - if root.Left!=nil{ - traversal(root.Left,result,pre) + func findMode(root *TreeNode) []int { + res := make([]int, 0) + count := 1 + max := 1 + var prev *TreeNode + var travel func(node *TreeNode) + travel = func(node *TreeNode) { + if node == nil { + return + } + travel(node.Left) + if prev != nil && prev.Val == node.Val { + count++ + } else { + count = 1 + } + if count >= max { + if count > max && len(res) > 0 { + res = []int{node.Val} + } else { + res = append(res, node.Val) + } + max = count + } + prev = node + travel(node.Right) } - if root.Right!=nil{ - traversal(root.Right,result,pre) - } + travel(root) + return res } ```