Skip to content

Commit

Permalink
Merge pull request #80 from Bhairavnath54/patch-2
Browse files Browse the repository at this point in the history
Binary Tree Preorder Traversal.cpp
  • Loading branch information
avastino7 authored Oct 31, 2022
2 parents 969a01c + 6368a6f commit c3b98d5
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Binary Tree Preorder Traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Given the root of a binary tree, return the preorder traversal of its nodes' values.
*/
class Solution {
public:
vector<int> ans;
void preorder(TreeNode* root){
if (root==NULL)return;
ans.push_back(root->val);
preorder(root->left);
preorder(root->right);
}
vector<int> preorderTraversal(TreeNode* root) {
// n l r
preorder(root);
return ans;
}
};

0 comments on commit c3b98d5

Please sign in to comment.