530. Minimum Absolute Difference in BST
来源:互联网 发布:制做图表的软件 编辑:程序博客网 时间:2024/06/05 10:13
Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.
Example:
Input: 1 \ 3 / 2Output:1Explanation: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.
我的解答,想得有点复杂,当时做的时候想的是,最小的差会有两种情况,一种式节点与其左子树中的最右子树的差值,以及与其右子树中的最左子树的差值。
写出来的比较复杂。
/** * 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 getMinimumDifference(TreeNode* root) { queue<TreeNode*> q; q.push(root); int rs = 0x7fffffff; while(!q.empty()){ TreeNode* temp = q.front(); q.pop(); rs = (rs < min(minLeftTree(temp), minRightLeft(temp)) ? rs : min(minLeftTree(temp), minRightLeft(temp))); if(temp->left) q.push(temp->left); if(temp->right) q.push(temp->right); } return rs; } //对于每个节点,和他差值最小的是min(左子树的最右子树节点,右子树的最左子树节点) int minLeftTree(TreeNode* root){ if(!root || !root->left){ return 0x7fffffff; } if(!root->left->right){ return root->val - root->left->val; } TreeNode* temp = root->left->right; while(temp->right){ temp = temp->right; } return root->val - temp->val; } int minRightLeft(TreeNode* root){ if(!root || !root->right){ return 0x7fffffff; } if(!root->right->left){ return root->right->val - root->val; } TreeNode* temp = root->right->left; while(temp->left){ temp = temp->left; } return temp->val - root->val; }};
最简单的方式,是用中序遍历,因为其为二叉搜索数,所以中序遍历的结果得到的节点便是一个从小递增的过程,
则此时最小的差值肯定出现在相邻的两个点之见。
/** * 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 getMinimumDifference(TreeNode* root) { int val = -1, min_diff = INT_MAX; subResult(root, val, min_diff); return min_diff; } void subResult(TreeNode* root, int& val, int& min_diff){ if(root->left){ subResult(root->left, val, min_diff); } if(val >= 0){ min_diff = min(min_diff, root->val - val); } val = root->val; if(root->right){ subResult(root->right, val, min_diff); } }};
阅读全文
0 0
- Leetcode-530. Minimum Absolute Difference in BST
- 【Leetcode】530. Minimum Absolute Difference in BST
- Leetcode 530. Minimum Absolute Difference in BST
- 530. Minimum Absolute Difference in BST
- 530. Minimum Absolute Difference in BST
- [leetcode] 530. Minimum Absolute Difference in BST
- 530. Minimum Absolute Difference in BST
- LeetCode-530. Minimum Absolute Difference in BST
- LeetCode 530. Minimum Absolute Difference in BST
- [LeetCode]530. Minimum Absolute Difference in BST
- 530. Minimum Absolute Difference in BST
- LeetCode | 530. Minimum Absolute Difference in BST
- leetcode 530. Minimum Absolute Difference in BST
- [leetcode]-530. Minimum Absolute Difference in BST
- 530. Minimum Absolute Difference in BST
- [LeetCode]530. Minimum Absolute Difference in BST
- 530. Minimum Absolute Difference in BST
- [leetcode]: 530. Minimum Absolute Difference in BST
- 那些年踩过的坑之(1)Hibernate自动创建表出错
- 二叉树的后序遍历
- 那些年踩过的坑(2) Hibernate 和 Struts 的javaassist.jar冲突
- 线程和进程详解
- JavaScript严格模式
- 530. Minimum Absolute Difference in BST
- ccna学习指南,入门必备
- Hibernate系列之(1) 抓取策略
- Wi-Fi产品中的智能天线设计
- 皮格马利翁效应
- mysql索引的实现原理
- Hibernate系列之(2)丢失更新解决
- C和指针第十一章 动态内存分配
- 字符串hash