Skip to content

Commit 1a94fd5

Browse files
committed
Move median_of_the_two_sorted_arrays.cpp to /leetcode/array, add path sum and maximum depth solution.
1 parent 061b3a6 commit 1a94fd5

File tree

7 files changed

+125
-4
lines changed

7 files changed

+125
-4
lines changed

leetcode/binary_tree/balanced_binary_tree.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Chacha
33
* @Date: 2019-01-03 17:07:54
44
* @Last Modified by: Chacha
5-
* @Last Modified time: 2019-01-03 18:01:39
5+
* @Last Modified time: 2019-02-15 15:12:59
66
*/
77

88
#include<iostream>
@@ -57,7 +57,7 @@ class Solution {
5757

5858
return max(leftDepth, rightDepth) + 1;
5959
}
60-
}
60+
};
6161

6262
int main() {
6363
return 0;

leetcode/binary_tree/maximum_depth

4.15 KB
Binary file not shown.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @Author: Chacha
3+
* @Date: 2019-02-16 13:26:00
4+
* @Last Modified by: Chacha
5+
* @Last Modified time: 2019-02-16 18:30:38
6+
*/
7+
8+
#include<iostream>
9+
#include<queue>
10+
using namespace std;
11+
12+
struct TreeNode {
13+
int val;
14+
TreeNode *left;
15+
TreeNode *right;
16+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
17+
};
18+
19+
class Solution {
20+
public:
21+
int maxDepth(TreeNode* root) {
22+
if (root == NULL) return 0;
23+
24+
queue<TreeNode *> q;
25+
q.push(root);
26+
27+
int max_depth = 0;
28+
29+
while(!q.empty()){
30+
int size = q.size();
31+
for(int i = 0; i < size; i++) {
32+
TreeNode *node = q.front();
33+
q.pop();
34+
35+
if (node->left) {
36+
q.push(node->left);
37+
}
38+
39+
if (node->right) {
40+
q.push(node->right);
41+
}
42+
}
43+
++max_depth;
44+
}
45+
46+
return max_depth;
47+
}
48+
};
49+
50+
int main() {
51+
return 0;
52+
}

leetcode/binary_tree/path_sum

4.15 KB
Binary file not shown.

leetcode/binary_tree/path_sum.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @Author: Chacha
3+
* @Date: 2019-02-15 15:12:02
4+
* @Last Modified by: Chacha
5+
* @Last Modified time: 2019-02-16 11:55:40
6+
*/
7+
8+
#include<iostream>
9+
#include<vector>
10+
using namespace std;
11+
12+
struct TreeNode {
13+
int val;
14+
TreeNode *left;
15+
TreeNode *right;
16+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
17+
};
18+
19+
class Solution {
20+
public:
21+
/**
22+
* Given a binary tree and a sum, determine if the tree has a root-to-leaf path such
23+
* that adding up all the values along the path equals the given sum.
24+
*
25+
* Example:
26+
* Given the below binary tree and sum = 22
27+
* 5
28+
/ \
29+
4 8
30+
/ / \
31+
11 13 4
32+
/ \ \
33+
7 2 1
34+
35+
* return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
36+
*
37+
* Source:
38+
* https://leetcode.com/problems/path-sum/
39+
*/
40+
bool hasPathSum(TreeNode* root, int sum) {
41+
if (root == NULL) return false;
42+
if (root->val == sum && root->left == NULL && root->right == NULL) return true;
43+
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
44+
}
45+
};
46+
47+
int main() {
48+
return 0;
49+
}
50+

leetcode/linked_list/two_lists_sum.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ class Solution {
2828
*
2929
* Example:
3030
* Given two lists, 3->1->5->null and 5->9->2->null, return 8->0->8->null
31+
*
32+
* Source:
33+
* https://www.lintcode.com/en/problem/two-lists-sum/
34+
* https://www.kancloud.cn/kancloud/data-structure-and-algorithm-notes/73006
3135
*/
32-
ListNode* twoListsSum(ListNode* l1, ListNode* l2) {
36+
ListNode* twoListsSum1(ListNode* l1, ListNode* l2) {
3337
if (l1 == NULL && l2 == NULL) {
3438
return NULL;
3539
}
@@ -59,6 +63,21 @@ class Solution {
5963

6064
return sumList;
6165
}
66+
67+
/**
68+
* Given two numbers represented by two linked lists, write a function that returns sum list.
69+
* The sum list is linked list representation of addition of two input numbers.
70+
*
71+
* Example:
72+
* Input:
73+
* First List: 5->6->3 // represents number 563
74+
* Second List: 8->4->2 // represents number 842
75+
* Output:
76+
* Resultant list: 1->4->0->5 // represents number 1405
77+
*/
78+
ListNode* twoListsSum2(ListNode* l1, ListNode* l2) {
79+
80+
}
6281
};
6382

6483
/* Function to print nodes in a given linked list */
@@ -79,7 +98,7 @@ int main()
7998
l2->next = new ListNode(9);
8099
l2->next->next = new ListNode(2);
81100

82-
ListNode* sum = Solution().twoListsSum(l1, l2);
101+
ListNode* sum = Solution().twoListsSum1(l1, l2);
83102

84103
printList(sum);
85104

0 commit comments

Comments
 (0)