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, cocould You Do It iteratively?
/*** Definition for binary tree * struct treenode {* int val; * treenode * left; * treenode * right; * treenode (int x): Val (x ), left (null), right (null) {}*}; * // The post-order traversal of the bintree struct treenode {int val; treenode * left; treenode * right; treenode (int x): Val (x), left (null), right (null) {}}; Class solution {public: STD :: vector <int> postordertraversal (treenode * root) {STD: vector <int> VEC; bintree (root, VEC); Re Turn VEC;} void bintree (treenode * root, STD: vector <int> & VEC) {If (root! = NULL) {bintree (root-> left, VEC); bintree (root-> right, VEC); Vec. push_back (root-> Val) ;}private: STD: vector <int> VEC ;};
Leetcode-binary tree postorder Traversal