• 0530二叉搜索树的最小绝对差 Marathon


    给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 。

    差值是一个正数,其数值等于两值之差的绝对值。

    示例 1:

    输入:root = [4,2,6,1,3]
    输出:1
    示例 2:

    输入:root = [1,0,48,null,null,12,49]
    输出:1

    提示:

    树中节点的数目范围是 [2, 104]
    0 <= Node.val <= 105

    注意:本题与 783 https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/ 相同

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst

    参考:

    python

    # 0530.二叉搜索树的最小绝对差
    
    # 递归-中序-数组比较
    class Solution:
        def getMinimumDifference(self, root: *TreeNode) -> int:
            res = []
            r = float("inf")
    
            def buildaList(root):  # 把二叉搜索树转换成有序数组
                if not root: return None
                if root.left: buildaList(root.left)  # 左
                res.append(root.val)  # 中
                if root.right: buildaList(root.right)  # 右
                return res
    
            buildaList(root)
            for i in range(len(res) - 1):  # 统计有序数组的最小差值
                r = min(abs(res[i] - res[i + 1]), r)
            return r
    
    
    class Solution1:
        def getMinimumDifference(self, root: *TreeNode) -> int:
            result = 1<<63-1
            pre = None
    
            def travel(node):
                nonlocal result, pre
                if not node:
                    return
    
                travel(node.left)
    
                if pre != None:
                    result = min(result, node.val-pre.val)
                pre = node # 记录前一个
    
                travel(node.right)
    
            travel(root)
            return result
    
    class Solution2:
        def getMinimumDifference(self, root: TreeNode) -> int:
            stack = []
            cur = root
            pre = None
            result = 1<<63-1
    
            while cur or stack:
                if cur: # 遍历至最左左节点
                    stack.append(cur)
                    cur = cur.left
                else: # 处理栈顶元素
                    cur = stack.pop()
                    if pre:
                        result = min(result, cur.val-pre.val)
                    pre = cur
                    cur = cur.right
    
            return result
    

    golang

    package binaryTree
    
    import "math"
    
    // 递归-中序-数组比较
    func getMinimumDifference1(root *TreeNode) int {
    	res := []int{}
    
    	var travel func(node *TreeNode)
    	travel = func(node *TreeNode) {
    		if node == nil {
    			return
    		}
    		travel(node.Left)
    		res = append(res, node.Val)
    		travel(node.Right)
    	}
    
    	travel(root)
    	if len(res) < 2 {
    		return 0
    	}
    	Min := math.MaxInt64
    	for i:=1;i<len(res);i++ {
    		temp := res[i] - res[i-1]
    		if Min > temp {
    			Min = temp
    		}
    	}
    	return Min
    }
    
    // 递归-中序前驱节点-利用BST的性质
    func getMinimumDifference2(root *TreeNode) int {
    	result := math.MaxInt64
    	var pre *TreeNode
    
    	var travel func(node *TreeNode)
    	travel = func(node *TreeNode) {
    		if node == nil {
    			return
    		}
    		travel(node.Left)
    		if pre != nil {
    			temp := node.Val - pre.Val
    			if result > temp {
    				result = temp
    			}
    		}
    		pre = node
    		travel(node.Right)
    	}
    
    	travel(root)
    	return result
    }
    
    // 迭代-中序遍历-有限变量处理
    func getMinimumDifference3(root *TreeNode) int {
    	stack := []*TreeNode{}
    	cur := root
    	var pre *TreeNode
    	result := math.MaxInt64
    
    	for cur != nil || len(stack) > 0 {
    		if cur != nil {
    			stack = append(stack, cur)
    			cur = cur.Left
    		} else {
    			cur = stack[len(stack)-1]
    			stack = stack[:len(stack)-1]
    			if pre != nil {
    				temp := cur.Val - pre.Val
    				if result > temp {
    					result = temp
    				}
    			}
    			pre = cur
    			cur = cur.Right
    		}
    	}
    	return result
    }
    
    
    
  • 相关阅读:
    idea_pyspark 环境配置
    Win7 单机Spark和PySpark安装
    Spark在Windows下的环境搭建
    linux 登陆key生成
    nginx 根据参数选择文档根目录
    系统操作日志设计(转)
    smarty、smarty格式化、smarty整数、smarty float、smarty各种转换方式、smarty日期转换等等 (转)
    Mac下面的SecureCRT(附破解方案) 更新到最新的7.3.2(转)
    nginx php-fpm 输出php错误日志
    解决PHP显示Warning和Notice等问题
  • 原文地址:https://www.cnblogs.com/davis12/p/15575091.html
Copyright © 2020-2023  润新知