144. Binary Tree Preorder Traversal LeetCode

来源:互联网 发布:linux查看crontab任务 编辑:程序博客网 时间:2024/05/21 17:48

题意:二叉树的前序遍历。
题解:

class Solution {public:    vector<int> ans;    void dfs(TreeNode* root)    {        if(root == NULL) return ;        ans.push_back(root->val);        if(root->left)             dfs(root->left);        if(root->right)            dfs(root->right);    }    vector<int> preorderTraversal(TreeNode* root) {        dfs(root);        return ans;    }};
0 0
原创粉丝点击