Minimum Absolute Difference in BST
来源:互联网 发布:windows win7 ios系统 编辑:程序博客网 时间:2024/06/04 23:22
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).
方法1: 对每个节点进行寻找。 前序遍历+寻找。
/** * 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) { TreeNode * cur ; int left,right; // left cur = root -> left; if(cur){ while(cur->right){ cur = cur->right; } left = abs(cur->val - root->val); } else left = -1; // right cur = root -> right; if(cur){ while(cur->left){ cur = cur->left; } right = abs(cur->val - root->val); } else right = -1; int min_diff ; if(left == -1 || right == -1){ if(left==-1 && right ==-1) min_diff = INT_MAX; else min_diff = (left==-1?right:left); } else{ min_diff = min(left,right); } if(root->left) min_diff = min(min_diff,getMinimumDifference(root->left)); if(root->right) min_diff = min(min_diff,getMinimumDifference(root->right)); return min_diff; }};
方法2:中序遍历,记录前一个节点。
/** * 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 {private: void traverse(TreeNode *root,int& last,int& diff){ if(root->left) traverse(root->left,last,diff); if(last == -1) last = root -> val; else{ diff = min(diff,abs(last - root->val)); last = root -> val; } if(root->right) traverse(root->right,last,diff); }public: int getMinimumDifference(TreeNode* root) { int diff = INT_MAX,last = -1; traverse(root,last,diff); return diff; }};
0 0
- Minimum Absolute Difference in BST
- Minimum Absolute Difference in BST
- Minimum Absolute Difference in BST
- Minimum Absolute Difference in BST
- Minimum Absolute Difference in BST
- Minimum Absolute Difference in BST
- Minimum Absolute Difference in BST
- Minimum Absolute Difference in BST
- Leetcode-530. Minimum Absolute Difference in BST
- LeetCode: 503.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
- [leetcode] 530. Minimum Absolute Difference in BST
- 530. Minimum Absolute Difference in BST
- LeetCode-530. Minimum Absolute Difference in BST
- 机器学习算法应用中常用技巧-2
- 数组与List的相互转换
- linux0.11中bootsect.s文件分析
- 使用 C++11 编写 Linux 多线程程序
- 修改安卓文件的权限
- Minimum Absolute Difference in BST
- 通过一个kaggle实例学习解决机器学习问题
- SecureCRT工具上传文件到Linux以及下载文件到本地(Windows)
- 页面获取服务器图片路径问题
- Angularjs API整理(二) directive篇(指令)
- 浅谈用webpack构建Vue
- mysql忘记密码
- 树状数组
- Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.itcast.j