Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
a81f28c
Maximum Depth of Binary Tree
ranger34 Aug 15, 2015
270fb54
Single Number
ranger34 Aug 15, 2015
eac288b
add explanation
ranger34 Aug 15, 2015
7746dcb
Delete Node in a Linked List
ranger34 Aug 15, 2015
348657f
Same Tree
ranger34 Aug 15, 2015
cd71e28
Add Digits
ranger34 Aug 16, 2015
faf239f
Number of 1 Bits
ranger34 Aug 16, 2015
399f77d
Lowest Common Ancestor of a Binary Search Tree
ranger34 Aug 16, 2015
d1e29c2
modified
ranger34 Aug 16, 2015
fdd9e5d
Invert Binary Tree
ranger34 Aug 16, 2015
5d33400
modified the implementation of invert binary tree
ranger34 Aug 16, 2015
8d958ac
Excel Sheet Column Number
ranger34 Aug 16, 2015
2198b95
Excel Sheet Column Title
ranger34 Aug 17, 2015
ebb9360
add another method
ranger34 Aug 17, 2015
aad90f4
Contains Duplicate
ranger34 Aug 17, 2015
3aae58d
Majority Element
ranger34 Aug 17, 2015
29fc982
Remove Duplicates from Sorted List
ranger34 Aug 17, 2015
56b92b5
Climbing Stairs
ranger34 Aug 18, 2015
0e84dc0
Roman to Integer
ranger34 Aug 18, 2015
d248a3c
Valid Anagram
ranger34 Aug 18, 2015
d250847
Implement Queue using Stacks
ranger34 Aug 18, 2015
ef507cf
Merge Two Sorted Lists
ranger34 Aug 18, 2015
ddbacc7
Reverse Linked List
ranger34 Aug 18, 2015
f733588
Happy Number
ranger34 Aug 18, 2015
f3f924f
Ugly Number
ranger34 Aug 19, 2015
0996c3e
Balanced Binary Tree
ranger34 Aug 19, 2015
da10a74
Remove Element
ranger34 Aug 19, 2015
babb64b
Symmetric Tree
ranger34 Aug 19, 2015
a018254
Remove Duplicates from Sorted Array
ranger34 Aug 19, 2015
ee3363e
Implement Stack using Queues
ranger34 Aug 19, 2015
94c061f
change to one queue
ranger34 Aug 19, 2015
2ea7cf4
Plus One
ranger34 Aug 23, 2015
1f63ddb
Pascal's Triangle
ranger34 Aug 23, 2015
04c4ab3
Power of Two
ranger34 Aug 23, 2015
891c092
Path Sum
ranger34 Aug 23, 2015
7cf2d0f
Pascal's Triangle II
ranger34 Aug 23, 2015
3ac7023
House Robber
ranger34 Aug 24, 2015
61824e4
Factorial Trailing Zeroes
ranger34 Aug 29, 2015
19b08fc
102
ranger34 Aug 29, 2015
c2e38df
107
ranger34 Aug 29, 2015
3088512
107
ranger34 Sep 4, 2015
cb36fa3
160
ranger34 Sep 4, 2015
fafb7fe
102
ranger34 Sep 4, 2015
353b037
88
ranger34 Sep 4, 2015
6e37c9f
190
ranger34 Sep 4, 2015
6314e74
111
ranger34 Sep 5, 2015
bfa82a1
9
ranger34 Sep 17, 2015
018d54c
283
ranger34 Sep 29, 2015
34f9244
58
ranger34 Sep 29, 2015
6cbd452
19
ranger34 Sep 29, 2015
8f29c3e
219
ranger34 Oct 2, 2015
c9fa006
add hash set
ranger34 Oct 2, 2015
f2b9be5
206
ranger34 Oct 9, 2015
2c9e736
203
ranger34 Oct 14, 2015
276737c
36
ranger34 Oct 14, 2015
cdbdb7a
20
ranger34 Oct 14, 2015
17628d6
14
ranger34 Oct 25, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions 100.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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:
bool isSameTree(TreeNode* p, TreeNode* q) {
//check the node itself
if(p==NULL && q==NULL) return true;
else
if(p==NULL||q==NULL) return false;
else
{
if(p->val != q->val) return false;
else
//check the child nodes
isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
}
};
95 changes: 95 additions & 0 deletions 101.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

1
/ \
2 2
/ \ / \
3 4 4 3

But the following is not:

1
/ \
2 2
\ \
3 3

Note:
Bonus points if you could solve it both recursively and iteratively.

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

1
/ \
2 3
/
4
\
5

The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
// use recursion
class Solution {
public:
bool DFS(TreeNode* left, TreeNode* right)
{
if(left == NULL && right == NULL) return true;
if(left == NULL || right == NULL) return false;
if(left->val != right->val) return false;
else
return DFS(left->left,right->right) && DFS(left->right, right->left);
}
bool isSymmetric(TreeNode* root) {
if(root == NULL) return true;
return DFS(root->left, root->right);
}
};

// use two queues to record the left and right branch
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root == NULL) return true;
TreeNode *left, *right;
queue<TreeNode*> q1, q2;
q1.push(root->left);
q2.push(root->right);
while(!q1.empty() && !q2.empty())
{
left = q1.front();
q1.pop();
right = q2.front();
q2.pop();

if(left == NULL && right == NULL) continue;
if(left == NULL || right == NULL) return false;
if(left->val != right->val) return false;
else
{
q1.push(left->left);
q1.push(left->right);
q2.push(right->right);
q2.push(right->left);
}
}
return true;
}
};
20 changes: 20 additions & 0 deletions 104.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//Maximum Depth of Binary Tree

/**
* 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 maxDepth(TreeNode* root) {
if (root == NULL)
return 0;
else
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
};
50 changes: 50 additions & 0 deletions 110.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/

// top to down
class Solution {
public:
int depth(TreeNode* root){
if(root == NULL)
return 0;
else
return max(depth(root->left), depth(root->right)) + 1;
}
bool isBalanced(TreeNode* root) {
if(root == NULL) return true;
int ld = depth(root->left);
int rd = depth(root->right);
if(abs(ld-rd) > 1)
return false;
else
return isBalanced(root->left) && isBalanced(root->right);
}
};

// bottom up
class Solution {
public:
//if not balance ,return -1; else return the depth
int balanceness(TreeNode* root){
if(root == NULL) return 0;
int ld = balanceness(root->left);
if(ld == -1) return -1;
int rd = balanceness(root->right);
if(rd == -1) return -1;
if(abs(ld - rd) > 1) return -1;
return max(ld, rd) + 1;
}
bool isBalanced(TreeNode* root) {
return balanceness(root) != -1;
}
};
31 changes: 31 additions & 0 deletions 112.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and sum = 22,

5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

/**
* 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:
bool hasPathSum(TreeNode* root, int sum) {
if(root == NULL) return false;
if(root->left == NULL && root->right == NULL && sum == root->val) return true;
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}
};
32 changes: 32 additions & 0 deletions 118.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> pascal;
pascal.resize(numRows);
for(int i = 1; i <= numRows; i++)
pascal[i - 1].resize(i);

for(int i = 0; i < numRows; i++)
for(int j = 0; j < i + 1; j++)
{
if(j == 0) pascal[i][j] = 1;
else
if(j == i) pascal[i][j] = 1;
else
pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
}

return pascal;
}
};
21 changes: 21 additions & 0 deletions 119.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,
Return [1,3,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

// use O(k) extra space
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> pascal(rowIndex + 1, 0);
pascal[0] = 1;

for(int i = 1; i < rowIndex + 1; i++)
for(int j = i; j >= 1; j--)
pascal[j] += pascal[j - 1];
return pascal;
}
};
33 changes: 33 additions & 0 deletions 13.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

// the Roman alphabet is listed in the switch statement below
class Solution {
public:
int decode(char c){
switch(c){
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
}
}

int romanToInt(string s) {
int n = 0;
int n1, n2;
for(int i = 0; i < s.size() - 1; i++)
{
n1 = decode(s[i]);
n2 = decode(s[i + 1]);
if(n1 < n2) n -= n1;
else n += n1;
}
n += decode(s[s.size()-1]);
return n;
}
};
14 changes: 14 additions & 0 deletions 136.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//Given an array of integers, every element appears twice except for one. Find that single one.

//Note:
//Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

// A ^ A = 0, 0 ^ A = 0, A ^ B ^ A = B
class Solution {
public:
int singleNumber(vector<int>& nums) {
for(int i = 1; i < nums.size(); i++)
nums[0] = nums[0] ^ nums[i];
return nums[0];
}
};
47 changes: 47 additions & 0 deletions 168.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:

1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB


class Solution {
public:
string convertToTitle(int n) {
string result, s_tmp;
char ch_tmp;
int tail = 0;
while(n > 0)
{
tail = n % 26;
if(tail == 0)
{
tail = 26;
result = "Z" + result;
}
else
{
ch_tmp = 'A' - 1 + tail;
s_tmp = ch_tmp;
result = s_tmp + result;
}
n = (n - tail)/26;
}

/*
while(n){
n -= 1;
ch_tmp = 'A' + n % 26;
result = ch_tmp + result;
n /= 26;
}
*/

return result;
}
};
Loading