LeetCode OJ - Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},

1 \ 2 / 3


return [1,2,3].
【LeetCode OJ - Binary Tree Preorder Traversal】 Note: Recursive solution is trivial, could 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) {} * }; */ class Solution { public: vector preorderTraversal(TreeNode *root) { vector rslt; preorder(root, rslt); return rslt; }void preorder(TreeNode *root, vector &rslt) { if(root) { rslt.push_back(root->val); preorder(root->left, rslt); preorder(root->right, rslt); } } };


    推荐阅读