Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

All level 1 in Leetcode...

  • Loading branch information...
commit 4418c279b5a57fec0d2a56b3d2e3ce9e69b59835 1 parent b62cee0
Charlene Jiang 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<TreeNode *> q;
  29
+        q.push(root);
  30
+        
  31
+        vector<TreeNode *> 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<int> &digits) {
  4
+        int b = 0, e = digits.size() - 1;
  5
+        while (b < e) swap(digits[b ++], digits[e --]);
  6
+    }
  7
+    vector<int> plusOne(vector<int> &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
... ...
@@ -0,0 +1,37 @@
  1
+/**
  2
+ * Definition for singly-linked list.
  3
+ * struct ListNode {
  4
+ *     int val;
  5
+ *     ListNode *next;
  6
+ *     ListNode(int x) : val(x), next(NULL) {}
  7
+ * };
  8
+ */
  9
+class Solution {
  10
+public:
  11
+    unordered_set<int> s;
  12
+    
  13
+    ListNode *deleteDuplicates(ListNode *head) {
  14
+        // Start typing your C/C++ solution below
  15
+        // DO NOT write int main() function
  16
+        
  17
+        if (!head || !head->next) return head;
  18
+        
  19
+        s.clear();
  20
+        s.insert(head->val);
  21
+        
  22
+        ListNode *node = head->next, *currentHead = head;
  23
+        while (node != NULL) {
  24
+            if (s.find(node->val) == s.end()) {
  25
+                s.insert(node->val); //don't forget to insert...
  26
+                currentHead->next = node;
  27
+                currentHead = node; //move head to the next...
  28
+            }
  29
+            
  30
+            node = node->next;
  31
+        }
  32
+        
  33
+        currentHead->next = NULL; //don't forget to set NULL... 
  34
+        
  35
+        return head;
  36
+    }
  37
+};
19  LeetCode/RemoveElement.cpp
... ...
@@ -0,0 +1,19 @@
  1
+class Solution {
  2
+public:
  3
+    int removeElement(int A[], int n, int elem) {
  4
+        // Start typing your C/C++ solution below
  5
+        // DO NOT write int main() function
  6
+        
  7
+        int b = -1, e = 0;
  8
+        
  9
+        while (e < n) {
  10
+            if (A[e] != elem) {
  11
+                A[++ b] = A[e];
  12
+            } 
  13
+            
  14
+            e ++;
  15
+        }
  16
+        
  17
+        return b + 1;
  18
+    }
  19
+};
23  LeetCode/SameTree.cpp
... ...
@@ -0,0 +1,23 @@
  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
+    bool isSameTree(TreeNode *p, TreeNode *q) {
  13
+        // Start typing your C/C++ solution below
  14
+        // DO NOT write int main() function
  15
+        
  16
+        //Watch out the sequence of the following three lines...
  17
+        if (!p && !q) return true;
  18
+        if ((!p && q) || (p && !q)) return false;
  19
+        if (p->val != q->val) return false;
  20
+        
  21
+        return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
  22
+    }
  23
+};
89  LeetCode/SymmetricTree.cpp
... ...
@@ -0,0 +1,89 @@
  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
+    bool isSymmetric(TreeNode *left, TreeNode *right) {
  14
+        if (!left && !right) return true;
  15
+        if (!left || !right) return false;
  16
+        if (left->val != right->val) return false;
  17
+        
  18
+        if (!isSymmetric(left->left, right->right)) return false;
  19
+        return isSymmetric(left->right, right->left);
  20
+    }
  21
+    
  22
+    bool isSymmetric(TreeNode *root) {
  23
+        // Start typing your C/C++ solution below
  24
+        // DO NOT write int main() function
  25
+        
  26
+        if (!root) return true;
  27
+        
  28
+        
  29
+        return isSymmetric(root->left, root->right);
  30
+    }
  31
+};
  32
+
  33
+/*
  34
+
  35
+// Unaccepted solution... BFS... why?
  36
+class Solution {
  37
+public:
  38
+
  39
+    bool isSymetric(const vector<TreeNode *> &level) {
  40
+        int b = 0, e = level.size() - 1;
  41
+        while (b < e) {
  42
+            if (!level[b] && !level[e]) {b ++; e --; continue;}
  43
+            if (!level[b] || !level[e]) return false;
  44
+            if (level[b ++]->val != level[e --]->val) return false; 
  45
+        }
  46
+            
  47
+        return true;
  48
+    }
  49
+    
  50
+    bool isSymmetric(TreeNode *root) {
  51
+        // Start typing your C/C++ solution below
  52
+        // DO NOT write int main() function
  53
+        
  54
+        if (!root || (!root->left && !root->right)) return true;
  55
+        
  56
+        queue<TreeNode*> q;
  57
+        q.push(root);
  58
+        
  59
+        vector<TreeNode *> level;
  60
+        while (!q.empty()) {
  61
+            while (!q.empty()) {
  62
+                level.push_back(q.front());
  63
+                q.pop();
  64
+            }
  65
+            
  66
+            if (!isSymetric(level)) return false;
  67
+            
  68
+            for (int i = 0; i < level.size(); i ++) {
  69
+                if (level[i]) {
  70
+                    if (level[i]->left) q.push(level[i]->left);
  71
+                    else q.push(NULL);
  72
+                    if (level[i]->right) q.push(level[i]->right);
  73
+                    else q.push(NULL);
  74
+                } else {
  75
+                    q.push(NULL); 
  76
+                    q.push(NULL);
  77
+                }
  78
+                
  79
+            }
  80
+            
  81
+            level.clear();
  82
+    
  83
+        }
  84
+        
  85
+        return true;
  86
+    }
  87
+};
  88
+
  89
+*/

0 notes on commit 4418c27

Please sign in to comment.
Something went wrong with that request. Please try again.