From 6368a6f216afe3ccd7a27743539c1b71f13725c9 Mon Sep 17 00:00:00 2001 From: Bhairavnath54 <117094948+Bhairavnath54@users.noreply.github.com> Date: Mon, 31 Oct 2022 19:55:25 +0530 Subject: [PATCH] Create Binary Tree Preorder Traversal.cpp recursive preorder traversal solution --- Binary Tree Preorder Traversal.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Binary Tree Preorder Traversal.cpp diff --git a/Binary Tree Preorder Traversal.cpp b/Binary Tree Preorder Traversal.cpp new file mode 100644 index 0000000..29cda5a --- /dev/null +++ b/Binary Tree Preorder Traversal.cpp @@ -0,0 +1,20 @@ +/* + +Given the root of a binary tree, return the preorder traversal of its nodes' values. + +*/ + class Solution { +public: + vector ans; + void preorder(TreeNode* root){ + if (root==NULL)return; + ans.push_back(root->val); + preorder(root->left); + preorder(root->right); + } + vector preorderTraversal(TreeNode* root) { + // n l r + preorder(root); + return ans; + } +};