Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 28 additions & 0 deletions lcof/面试题25. 合并两个排序的链表/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,34 @@ func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
}
```

### **C++**

```cpp
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (nullptr == l1 && nullptr == l2) {
return nullptr; // 两个都为空,则直接返回
}

if (nullptr == l1 || nullptr == l2) {
return l1 == nullptr ? l2 : l1; // 有且仅有一个为空,则返回非空节点
}

ListNode* node = nullptr;
if (l1->val > l2->val) {
node = l2;
node->next = mergeTwoLists(l1, l2->next);
} else {
node = l1;
node->next = mergeTwoLists(l1->next, l2);
}

return node;
}
};
```

### **...**

```
Expand Down
32 changes: 32 additions & 0 deletions lcof/面试题25. 合并两个排序的链表/Solurion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/

class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (nullptr == l1 && nullptr == l2) {
return nullptr;
}

if (nullptr == l1 || nullptr == l2) {
return l1 == nullptr ? l2 : l1;
}

ListNode* node = nullptr;
if (l1->val > l2->val) {
node = l2;
node->next = mergeTwoLists(l1, l2->next);
} else {
node = l1;
node->next = mergeTwoLists(l1->next, l2);
}

return node;
}
};
47 changes: 47 additions & 0 deletions lcof/面试题26. 树的子结构/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,53 @@ func helper(a *TreeNode, b *TreeNode) bool {
}
```


### **C++**

```cpp
class Solution {
public:
bool isSubTree(TreeNode* a, TreeNode* b) {
if (nullptr == b) {
// 如果小树走到头,则表示ok了
return true;
}

if (nullptr == a) {
// 如果大树走到头,小树却没走到头,说明不对了
return false;
}

if (a->val != b->val) {
return false;
}

return isSubTree(a->left, b->left) && isSubTree(a->right, b->right);
}

bool isSubStructure(TreeNode* a, TreeNode* b) {
bool ret = false;
if (nullptr != a && nullptr != b) {
// 题目约定,空树不属于任何一个数的子树
if (a->val == b->val) {
// 如果值相等,才进入判定
ret = isSubTree(a, b);
}

if (false == ret) {
ret = isSubStructure(a->left, b);
}

if (false == ret) {
ret = isSubStructure(a->right, b);
}
}

return ret;
}
};
```

### **...**

```
Expand Down
49 changes: 49 additions & 0 deletions lcof/面试题26. 树的子结构/Sulotion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* 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 isSubTree(TreeNode* a, TreeNode* b) {
if (nullptr == b) {
// 如果小树走到头,则表示ok了
return true;
}

if (nullptr == a) {
// 如果大树走到头,小树却没走到头,说明不对了
return false;
}

if (a->val != b->val) {
return false;
}

return isSubTree(a->left, b->left) && isSubTree(a->right, b->right);
}

bool isSubStructure(TreeNode* a, TreeNode* b) {
bool ret = false;
if (nullptr != a && nullptr != b) {
if (a->val == b->val) {
// 如果值相等,才进入判定
ret = isSubTree(a, b);
}

if (false == ret) {
ret = isSubStructure(a->left, b);
}

if (false == ret) {
ret = isSubStructure(a->right, b);
}
}

return ret;
}
};