530. 平衡二叉树中,相邻节点的最小差 Minimum Absolute Difference in BST

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).

Note: There are at least two nodes in this BST.

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * public int val;
  5. * public TreeNode left;
  6. * public TreeNode right;
  7. * public TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public int GetMinimumDifference(TreeNode root) {
  12. if (root == null) {
  13. return 0;
  14. }
  15. int min = Int32.MaxValue;
  16. Stack<TreeNode> stack = new Stack<TreeNode>();
  17. TreeNode current = root;
  18. while (current != null) {
  19. stack.Push(current);
  20. current = current.left;
  21. }
  22. while (stack.Count != 0) {
  23. current = stack.Pop();
  24. if (stack.Count > 0) {
  25. int diff = Math.Abs(stack.Peek().val - current.val);
  26. min = Math.Min(diff,min);
  27. }
  28. TreeNode node = current.right;
  29. while (node != null) {
  30. stack.Push(node);
  31. node = node.left;
  32. if (stack.Count > 0) {
  33. int diff = Math.Abs(stack.Peek().val - current.val);
  34. min = Math.Min(diff,min);
  35. }
  36. }
  37. }
  38. return min;
  39. }
  40. }

null

时间: 2024-08-24 18:48:00

530. 平衡二叉树中,相邻节点的最小差 Minimum Absolute Difference in BST的相关文章

530. Minimum Absolute Difference in BST

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 (o

[LeetCode] 530. Minimum Absolute Difference in BST Java

题目: 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

Leetcode 530. Minimum Absolute Difference in BST

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 (o

LeetCode 530. Minimum Absolute Difference in BST(在二叉查找树中查找两个节点之差的最小绝对值)

题意:在二叉查找树中查找两个节点之差的最小绝对值 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int ans = 0x3f3f3f3f; TreeNo

【easy】530. Minimum Absolute Difference in BST

找BST树中节点之间的最小差值. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ //BST树中序遍历就是递增的序列,相邻两个数的差值,找最小 class Solution { public: int m

530. Minimum Absolute Difference in BST(LeetCode)

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 (o

[LeetCode&amp;Python] Problem 530. Minimum Absolute Difference in BST

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 (o

1.7交换链表中的相邻节点

交换链表中的相邻节点 题目描述: 把链表相邻元素翻转,例如给定链表为1-->2一>3一>4一>5-->6一>7,则翻转后的链表变为2一>1一>4一>3一>6一>5一>7 解题思路: 就地逆序法: 通过调整结点指针域的指向来直接调换相邻的两个结点.如果单链表恰好有偶数个结点,那么只需要将奇偶结点对调即可,如果链表有奇数个结点,那么只需要将除最后一个结点外的其它结点进行奇偶对调即可. 代码实现: # -*-coding:utf-8-*-

Hihocoder #1121 二分图一?二分图判定( bfs或者dfs搜索实现 搜索的过程中进行 节点标记 *【模板】)

对于拿到的相亲情况表,我们不妨将其转化成一个图.将每一个人作为一个点(编号1..N),若两个人之间有一场相亲,则在对应的点之间连接一条无向边.(如下图) 因为相亲总是在男女之间进行的,所以每一条边的两边对应的人总是不同性别.假设表示男性的节点染成白色,女性的节点染色黑色.对于得到的无向图来说,即每一条边的两端一定是一白一黑.如果存在一条边两端同为白色或者黑色,则表示这一条边所表示的记录有误. 由于我们并不知道每个人的性别,我们的问题就转化为判定是否存在一个合理的染色方案,使得我们所建立的无向图满