From 4c56f6c670c7e3a7fbf953f4f061a679e3fb08d9 Mon Sep 17 00:00:00 2001 From: Anik Mondal <55195279+anik2000s@users.noreply.github.com> Date: Tue, 4 Oct 2022 17:38:22 +0530 Subject: [PATCH] Create 94-Binary-Tree-Inorder-Traversal --- .../94-Binary-Tree-Inorder-Traversal | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Leetcode problems/94-Binary-Tree-Inorder-Traversal diff --git a/Leetcode problems/94-Binary-Tree-Inorder-Traversal b/Leetcode problems/94-Binary-Tree-Inorder-Traversal new file mode 100644 index 00000000..0cebbcb3 --- /dev/null +++ b/Leetcode problems/94-Binary-Tree-Inorder-Traversal @@ -0,0 +1,33 @@ +/** + * 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) {} + * }; + */ +class Solution { +public: + void inorder(TreeNode* root, vector &v){ + if(root->left!=NULL){ + inorder(root->left, v); + } + v.push_back(root->val); + if(root->right!=NULL){ + inorder(root->right, v); + } + } + + + vector inorderTraversal(TreeNode* root) { + vector v; + if(root==NULL){ + return v; + } + inorder(root,v); + return v; + } +};