来源:Lintcode
原帖:http://www.lintcode.com/en/problem/first-bad-version/
题目:
The code base version is an integer and start from 0 to n. One day, someone commit a bad version in the code case, so it caused itself and the following versions are all failed in the unit tests. You can determine whether a version is bad by the following interface: boolean isBadVersion(int version);
Find the first bad version.
代码:
原帖:http://www.lintcode.com/en/problem/first-bad-version/
题目:
The code base version is an integer and start from 0 to n. One day, someone commit a bad version in the code case, so it caused itself and the following versions are all failed in the unit tests. You can determine whether a version is bad by the following interface: boolean isBadVersion(int version);
Find the first bad version.
代码:
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int v) : val(v), left(NULL), right(NULL) {}
};
void find_k_largest_in_BST_helper(TreeNode* root, int k, vector<int> &k_elements) {
if (!root || k_element.size() == k) {
return;
}
// Perform reverse inorder traversal
if (root && k_elements.size() < k) {
find_k_largest_in_BST_helper(root->right, k, k_elements);
if (k_elements.size() < k) {
k_elements.push_back(root->val);
find_k_largest_in_BST_helper(root->left, k, k_elements);
}
}
}
vector<int> find_k_largest_in_BST(TreeNode* root, int k) {
vector<int> k_elements;
find_k_largest_in_BST_helper(root, k , k_elements);
return k_elements;
}
No comments:
Post a Comment