-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from Bhairavnath54/patch-2
Binary Tree Preorder Traversal.cpp
- Loading branch information
Showing
1 changed file
with
20 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |