diff --git "a/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/README.md" index a996712352134..fb75cda8da03d 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/README.md" @@ -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; + } +}; +``` + ### **...** ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/Solurion.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/Solurion.cpp" new file mode 100644 index 0000000000000..3b853e138a948 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/Solurion.cpp" @@ -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; + } +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/README.md" index ad8ca48f30108..7eaae533d6689 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/README.md" @@ -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; + } +}; +``` + ### **...** ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/Sulotion.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/Sulotion.cpp" new file mode 100644 index 0000000000000..cf671cdec3e4c --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/Sulotion.cpp" @@ -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; + } +}; \ No newline at end of file