530. Minimum Absolute Difference in BST

Question

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.

Example:

Input:

   1
    \
     3
    /
   2

Output:
1

Explanation:

The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).

   2
    \
     4443
    /
   1329
     \
     2917
      \
      4406

The minimum absolute difference is 37, which is the difference between 4443 and 4406.

代码实现

1) we need compare nearby
2) every root to rightest of left subtree, every root to leftest of right subtree.
this method beats 97% of submission.

public class Solution {
            public int GetMinimumDifference(TreeNode root)
            {
               //we need compare root to root.right, root to root.left, 
               //every root to rightest of left subtree, every root to leftest of right subtree
                int min = getNearby(root);
                return Math.Min(min,getRootDiff(root));                            
            }

            //we need compare root to root.right, root to root.left, 
            private int getNearby(TreeNode root)
            {
                if (root.left == null && root.right == null) return Int32.MaxValue;
                int min = 0;
                if (root.left != null)
                {
                    min = root.val - root.left.val;
                    min = Math.Min(min, getNearby(root.left));
                }
                if (root.right != null)
                {
                    if (root.left != null)
                    {
                        min = Math.Min(min, root.right.val - root.val);
                        min = Math.Min(min, getNearby(root.right));
                    }
                    else
                    {
                        min = root.right.val - root.val;
                        min = Math.Min(min, getNearby(root.right));
                    }
                }
                return min;
            }
            //every root to rightest of left subtree, every root to leftest of right subtree.
            private int getRootDiff(TreeNode root)
            {            
                int min = Int32.MaxValue;
                if (root == null) return min;
                if (root.left == null && root.right == null) return min;
                var node = root;
                //root to rightest of left subtree
                if (root.left != null)
                {
                    node = node.left;
                    while (node.right != null) node = node.right;
                    min = Math.Min(Math.Abs(root.val - node.val), min);
                }

                //root to leftest of right subtree
                if (root.right != null)
                {
                    node = root.right;
                    while (node.left != null) node = node.left;
                    min = Math.Min(Math.Abs(node.val - root.val), min);
                }
                min = Math.Min(min, getRootDiff(root.left));
                min = Math.Min(min, getRootDiff(root.right));
                return min;
            }

}

题库

leetcode算法题目解决方案每天更新在github库中,欢迎感兴趣的朋友加入进来,也欢迎star,或pull request。https://github.com/jackzhenguo/leetcode-csharp

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值