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 &nodes,TreeNode *node){ if(node->left) findNode(nodes,node->left); if(node->right) findNode(nodes,node->right); nodes.push_back(node->val); return; } vector postorderTraversal(TreeNode *root) { vector nodes; if(!root) return nodes; findNode(nodes,root); return nodes; }};