-
Notifications
You must be signed in to change notification settings - Fork 0
/
144-binary-tree-preorder-traversal.cpp
40 lines (34 loc) · 1.22 KB
/
144-binary-tree-preorder-traversal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Title: Binary Tree Preorder Traversal
// Description:
// Given the root of a binary tree, return the preorder traversal of its nodes' values.
// Link: https://leetcode.com/problems/binary-tree-preorder-traversal/
// Time complexity: O(n)
// Space complexity: O(n)
/**
* 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:
// iterative DFS (Depth First Search)
std::vector<int> preorderTraversal(TreeNode *root) {
std::stack<TreeNode *> processingStack; {
if (root != NULL) processingStack.push(root);
}
std::vector<int> result;
while (!processingStack.empty()) {
TreeNode *node = processingStack.top(); processingStack.pop();
result.push_back(node->val);
if (node->right != NULL) processingStack.push(node->right);
if (node->left != NULL) processingStack.push(node->left);
}
return result;
}
};