binary-tree-postorder-traversal 1.0.0
Binary Tree Postorder Traversal
Loading...
Searching...
No Matches
Solution Class Reference

Public Member Functions

vector< int > postorderTraversal (TreeNode *root)
 

Detailed Description

Definition at line 21 of file main.cpp.

Member Function Documentation

◆ postorderTraversal()

vector< int > Solution::postorderTraversal ( TreeNode root)
inline

Definition at line 24 of file main.cpp.

25 {
26 vector<int> answer = {};
27 if (root)
28 {
29 stack<TreeNode*> node_stack;
30 stack<TreeNode*> output_stack;
31 node_stack.push(root);
32 while (!node_stack.empty())
33 {
34 TreeNode* current = node_stack.top();
35 node_stack.pop();
36 output_stack.push(current);
37 if (current->left)
38 node_stack.push(current->left);
39 if (current->right)
40 node_stack.push(current->right);
41 }
42 while (!output_stack.empty())
43 {
44 answer.push_back(output_stack.top()->val);
45 output_stack.pop();
46 }
47 }
48 return answer;
49 }
Definition for a binary tree node.
Definition main.cpp:12
TreeNode * left
Definition main.cpp:14
TreeNode * right
Definition main.cpp:15

References TreeNode::left, and TreeNode::right.

Referenced by main().


The documentation for this class was generated from the following file: