Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 2 / 3
return [3,2,1]
.
Note: Recursive solution is trivial, could you do it iteratively?
Code:
class Solution { public: void findNode(vector<int> &nodes,TreeNode *node){ if(node->left) findNode(nodes,node->left); if(node->right) findNode(nodes,node->right); nodes.push_back(node->val); return; } vector<int> postorderTraversal(TreeNode *root) { vector<int> nodes; if(!root) return nodes; findNode(nodes,root); return nodes; } };