From 8031c03004aa0429089bff2cb1ce5c8f23f666e7 Mon Sep 17 00:00:00 2001 From: "junfeng.fj" Date: Sat, 30 Jan 2021 22:33:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86[lcof-27.?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E9=95=9C=E5=83=8F]?= =?UTF-8?q?=E4=B8=AD=E7=9A=84cpp=E7=89=88=E6=9C=AC=E9=A2=98=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Solution.cpp" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/Solution.cpp" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/Solution.cpp" new file mode 100644 index 0000000000000..8a98dc1458e14 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/Solution.cpp" @@ -0,0 +1,26 @@ +/** + * 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: + TreeNode* mirrorTree(TreeNode* root) { + // 后续遍历 + if (nullptr == root) { + return nullptr; + } + + mirrorTree(root->left); + mirrorTree(root->right); + std::swap(root->left, root->right); + + return root; + + } +}; From 14a6ab2536f3662dc99bec0b4eebf85a501f2053 Mon Sep 17 00:00:00 2001 From: "junfeng.fj" Date: Sat, 30 Jan 2021 22:46:47 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86[lcof-27.?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E9=95=9C=E5=83=8F]=E5=AF=B9?= =?UTF-8?q?=E5=BA=94=E7=9A=84readme=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../README.md" | 31 +++++++++++++++++++ .../Solution.cpp" | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/README.md" index a8e2df881607e..a4d1cb3cbf921 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/README.md" @@ -129,6 +129,37 @@ func mirrorTree(root *TreeNode) *TreeNode { } ``` +### **C++** + +```cpp +/** + * 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: + TreeNode* mirrorTree(TreeNode* root) { + // 后续遍历 + if (nullptr == root) { + return nullptr; + } + + mirrorTree(root->left); + mirrorTree(root->right); + std::swap(root->left, root->right); + + return root; + } +}; + +``` + ### **...** ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/Solution.cpp" index 346f9056aa7a7..33360a0a85f8c 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/Solution.cpp" @@ -31,7 +31,7 @@ class Solution { } } - return false; // 如果 + return false; // 如果有一处已经确定不是平衡二叉树了,则直接返回false } bool isBalanced(TreeNode* root) {