Binary Tree Preorder Traversal -- LEETCODE 144

方法一:(迭代)

class Solution
{
public:
vector<int> preorderTraversal(TreeNode* root)
{
vector<int> v; if(root == NULL) return v; stack<TreeNode *> s;
s.push(root); TreeNode *curNode = NULL;
while(!s.empty())
{
curNode = s.top(); s.pop();
v.push_back(curNode->val); // 注意这里的顺序
if(curNode->right) s.push(curNode->right);
if(curNode->left) s.push(curNode->left);
} return v;
}
};

方法二:(递归)

上一篇:weblogic 12C 数据源配置出错的解决办法


下一篇:HDU-4902 Nice boat