# publicCharleneJiang/InterviewPuzzle

### Subversion checkout URL

You can clone with HTTPS or Subversion.

All level 1 in Leetcode...

commit 4418c279b5a57fec0d2a56b3d2e3ce9e69b59835 1 parent b62cee0
authored April 02, 2013
41  LeetCode/BalancedBinaryTree.cpp
 ... ... @@ -0,0 +1,41 @@ 1 +/** 2 + * Definition for binary tree 3 + * struct TreeNode { 4 + * int val; 5 + * TreeNode *left; 6 + * TreeNode *right; 7 + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 + * }; 9 + */ 10 +class Solution { 11 +public: 12 + 13 + void dfs(TreeNode *root, int depth, int &height) 14 + { 15 + if (!root) return; 16 + 17 + if (!root->left && !root->right) { 18 + height = max(height, depth + 1); 19 + } 20 + 21 + dfs(root->left, depth + 1, height); 22 + dfs(root->right, depth + 1, height); 23 + } 24 + 25 + bool isBalanced(TreeNode *root) { 26 + // Start typing your C/C++ solution below 27 + // DO NOT write int main() function 28 + 29 + if (!root || (!root->left && !root->right)) return true; 30 + 31 + int leftHeight = 0, rightHeight = 0; 32 + 33 + dfs(root->left, 0, leftHeight); 34 + dfs(root->right, 0, rightHeight); 35 + 36 + if( abs(leftHeight - rightHeight) > 1) return false; 37 + 38 + return isBalanced(root->left) && isBalanced(root->right); 39 + 40 + } 41 +};
18  LeetCode/LengthOfLastWord.cpp
 ... ... @@ -0,0 +1,18 @@ 1 +class Solution { 2 +public: 3 + int lengthOfLastWord(const char *s) { 4 + // Start typing your C/C++ solution below 5 + // DO NOT write int main() function 6 + int e = strlen(s) - 1; 7 + int len = 0; 8 + 9 + while (e >= 0 && s[e] == ' ') e --; 10 + 11 + while (e >= 0 && s[e] != ' ') { 12 + len ++; 13 + e --; 14 + } 15 + 16 + return len; 17 + } 18 +};
34  LeetCode/MaximumDepthOfBinaryTree.cpp
 ... ... @@ -0,0 +1,34 @@ 1 +/** 2 + * Definition for binary tree 3 + * struct TreeNode { 4 + * int val; 5 + * TreeNode *left; 6 + * TreeNode *right; 7 + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 + * }; 9 + */ 10 +class Solution { 11 +public: 12 + int ans; 13 + void maxDepth(TreeNode *root, int depth) 14 + { 15 + if (!root){ 16 + ans = max(ans, depth); 17 + return; 18 + } 19 + 20 + maxDepth(root->left, depth + 1); 21 + maxDepth(root->right, depth + 1); 22 + } 23 + 24 + int maxDepth(TreeNode *root) { 25 + // Start typing your C/C++ solution below 26 + // DO NOT write int main() function 27 + 28 + ans = 0; //forgot to initialized... 29 + 30 + maxDepth(root, 0); 31 + 32 + return ans; 33 + } 34 +};
67  LeetCode/MinimumDepthOfBinrayTree.cpp
 ... ... @@ -0,0 +1,67 @@ 1 +/** 2 + * Definition for binary tree 3 + * struct TreeNode { 4 + * int val; 5 + * TreeNode *left; 6 + * TreeNode *right; 7 + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 + * }; 9 + */ 10 +class Solution { 11 +public: 12 + int ans; 13 + void minDepth(TreeNode *root, int depth) { 14 + if (!root) { //Although there is not the case 15 + return; 16 + } 17 + 18 + if (!root->left && !root->right) { //only leaf, could be the end of the path 19 + ans = min(ans, depth + 1); 20 + return; 21 + } 22 + 23 + minDepth(root->left, depth + 1); 24 + minDepth(root->right, depth + 1); 25 + } 26 + 27 + void bfs(TreeNode *root) { 28 + queue q; 29 + q.push(root); 30 + 31 + vector tmp; 32 + int level = 1; 33 + while (!q.empty()) { 34 + while (!q.empty()) { 35 + tmp.push_back(q.front()); 36 + q.pop(); 37 + } 38 + 39 + for (int i = 0; i < tmp.size(); i ++) { 40 + if (!tmp[i]->left && !tmp[i]->right) { 41 + ans = min(ans, level); 42 + return; 43 + } 44 + 45 + if (tmp[i]->left) q.push(tmp[i]->left); 46 + if (tmp[i]->right) q.push(tmp[i]->right); 47 + } 48 + 49 + level ++; 50 + tmp.clear(); //Don't forget to clear()! 51 + } 52 + } 53 + 54 + int minDepth(TreeNode *root) { 55 + // Start typing your C/C++ solution below 56 + // DO NOT write int main() function 57 + 58 + if (!root) return 0; 59 + if (!root->left && !root->right) return 1; 60 + 61 + ans = INT_MAX; 62 + minDepth(root, 0); //56 milli secs 63 + //bfs(root); //56 milli secs 64 + 65 + return ans; 66 + } 67 +};
32  LeetCode/PathSum.cpp
 ... ... @@ -0,0 +1,32 @@ 1 +/** 2 + * Definition for binary tree 3 + * struct TreeNode { 4 + * int val; 5 + * TreeNode *left; 6 + * TreeNode *right; 7 + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 + * }; 9 + */ 10 +class Solution { 11 +public: 12 + int target; 13 + 14 + bool isOK(TreeNode *root, int sum) { 15 + if (!root) return false; 16 + 17 + sum += root->val; 18 + //should always remember, it is leaf here: !root->left && !root->right 19 + if (sum == target && (!root->left && !root->right)) return true; 20 + 21 + return isOK(root->left, sum) || isOK(root->right, sum); 22 + } 23 + 24 + bool hasPathSum(TreeNode *root, int sum) { 25 + // Start typing your C/C++ solution below 26 + // DO NOT write int main() function 27 + 28 + target = sum; 29 + 30 + return isOK(root, 0); 31 + } 32 +};
26  LeetCode/PlusOne.cpp
 ... ... @@ -0,0 +1,26 @@ 1 +class Solution { 2 +public: 3 + void reverse(vector &digits) { 4 + int b = 0, e = digits.size() - 1; 5 + while (b < e) swap(digits[b ++], digits[e --]); 6 + } 7 + vector plusOne(vector &digits) { 8 + // Start typing your C/C++ solution below 9 + // DO NOT write int main() function 10 + reverse(digits); 11 + 12 + int delta = 1; 13 + for (int i = 0; i < digits.size(); i ++) { 14 + digits[i] = digits[i] + delta; 15 + if (digits[i] >= 10) { 16 + delta = 1; 17 + digits[i] -= 10; 18 + } else delta = 0; 19 + } 20 + 21 + if (delta > 0) digits.push_back(delta); 22 + reverse(digits); 23 + 24 + return digits; 25 + } 26 +};
37  LeetCode/RemoveDuplicatesFromSortedList.cpp