forked from weiliu89/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:haoel/leetcode
- Loading branch information
Showing
5 changed files
with
230 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Source : https://leetcode.com/problems/binary-tree-paths/ | ||
// Author : Calinescu Valentin | ||
// Date : 2015-10-23 | ||
|
||
/*************************************************************************************** | ||
* | ||
* Given a binary tree, return all root-to-leaf paths. | ||
* | ||
* For example, given the following binary tree: | ||
* | ||
* 1 | ||
* / \ | ||
* 2 3 | ||
* \ | ||
* 5 | ||
* | ||
* All root-to-leaf paths are: | ||
* ["1->2->5", "1->3"] | ||
* | ||
* Credits: | ||
* Special thanks to @jianchao.li.fighter for adding this problem and creating all test | ||
* cases. | ||
* | ||
***************************************************************************************/ | ||
/** | ||
* 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: | ||
vector<string> TreePaths; | ||
void DFS(TreeNode* node, string answer) | ||
{ | ||
answer += "->" + to_string(node->val); | ||
if(node->left == NULL && node->right == NULL) | ||
TreePaths.push_back(answer); | ||
else | ||
{ | ||
if(node->left != NULL) | ||
DFS(node->left, answer); | ||
if(node->right != NULL) | ||
DFS(node->right, answer); | ||
} | ||
} | ||
vector<string> binaryTreePaths(TreeNode* root) { | ||
if(root != NULL) | ||
{ | ||
DFS(root, ""); | ||
for(int i = 0; i < TreePaths.size(); i++) | ||
TreePaths[i].erase(TreePaths[i].begin(), TreePaths[i].begin() + 2); | ||
} | ||
return TreePaths; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Source : https://leetcode.com/problems/h-index-ii/ | ||
// Author : Calinescu Valentin | ||
// Date : 2015-10-23 | ||
|
||
/*************************************************************************************** | ||
* | ||
* Follow up for H-Index: What if the citations array is sorted in ascending order? | ||
* Could you optimize your algorithm? | ||
* | ||
***************************************************************************************/ | ||
|
||
|
||
|
||
/* | ||
* Solutions | ||
* ========= | ||
* | ||
* At every step we need to check whether this element is not less than | ||
* the remaining number of elements bigger than it(including itself) and all the values of | ||
* the other elements smaller than it are not more than that number. The h_index is this | ||
* number of elements bigger than it(including itself). | ||
* | ||
* Time Complexity: O(N) | ||
* Space Complexity: O(1) | ||
* | ||
*/ | ||
class Solution { | ||
public: | ||
int hIndex(vector<int>& citations) { | ||
int h_index = 0; | ||
for(int i = citations.size() - 1; i >= 0; i--) | ||
if(citations[i] >= citations.size() - i && (i - 1 < 0 || citations[i - 1] <= citations.size() - i)) | ||
h_index = citations.size() - i; | ||
return h_index; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters