From da93f6229e4b68823205f7b219216feb74884a62 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Sat, 18 Feb 2023 22:02:46 +0800 Subject: [PATCH] add: 230218 --- cpp/2302/230218.cpp | 21 +++++++++++++++++++++ cpp/CMakeLists.txt | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 cpp/2302/230218.cpp diff --git a/cpp/2302/230218.cpp b/cpp/2302/230218.cpp new file mode 100644 index 0000000..6f1c99e --- /dev/null +++ b/cpp/2302/230218.cpp @@ -0,0 +1,21 @@ +#include + +struct TreeNode { + int val; + TreeNode* left; + TreeNode* right; + + explicit TreeNode(int x, TreeNode* left = nullptr, TreeNode* right = nullptr) : val(x), left(left), right(right) {} +}; + +/** + * 226. Invert Binary Tree + * Given the root of a binary tree, invert the tree, and return its root. + */ + +class Solution { +public: + static TreeNode* invertTree(TreeNode* root) { + return root ? (std::swap(root->left, root->right), invertTree(root->left), invertTree(root->right), root) : nullptr; + } +}; diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index f3e632d..2231ba5 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -16,7 +16,7 @@ ENDIF() # Optimisation # SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Ofast -march=native -mtune=native -finline-functions -ffast-math -fomit-frame-pointer") -ADD_EXECUTABLE(leetcode-cpp main.cpp) +ADD_EXECUTABLE(leetcode-cpp main.cpp 2302/230218.cpp) # ADD_SUBDIRECTORY(2112) # ADD_SUBDIRECTORY(2201)