From 71c33bad96c2cde34371be3e226ee1abd362b487 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Sat, 9 Dec 2023 17:18:17 +0530 Subject: [PATCH] Create 94. Binary Tree Inorder Traversal --- 94. Binary Tree Inorder Traversal | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 94. Binary Tree Inorder Traversal diff --git a/94. Binary Tree Inorder Traversal b/94. Binary Tree Inorder Traversal new file mode 100644 index 0000000..6d6d61a --- /dev/null +++ b/94. Binary Tree Inorder Traversal @@ -0,0 +1,46 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ + #include +#pragma GCC optimize("O3") +#pragma GCC optimize("Ofast", "inline", "ffast-math", "unroll-loops", "no-stack-protector") +#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native", "f16c") +static const auto DPSolver = []() +{ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 'c'; }(); +using namespace std; + +class Solution { +public: + +void inorderTrav(TreeNode*node, vector&res){ + if(!node) + return; + if (!node->left && !node->right) + { + res.push_back(node->val); + return; + } + if (node->left) + inorderTrav(node->left, res); + res.push_back(node->val); + if (node->right) + inorderTrav(node->right, res); + +} + +vector inorderTraversal(TreeNode *root) +{ + DPSolver; + vector res; + inorderTrav(root, res); + return res; +} +};