前序遍历二叉树

binary-tree-preorder-traversal

https://www.nowcoder.com/practice/501fb3ca49bb4474bf5fa87274e884b4?tpId=46&tqId=29036&tPage=1&rp=1&ru=%2Fta%2Fleetcode&qru=%2Fta%2Fleetcode%2Fquestion-ranking

利用递归简单的实现:

class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> res;
        helper(root, res);
        return res;
    }
private:    
    void helper(TreeNode * root ,vector<int>& res) {
        if(root == NULL)return ;
        res.push_back(root->val);
        helper(root->left , res);
        helper(root->right,  res);

    }
};
全部评论

相关推荐

点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-01 11:27
点赞 评论 收藏
分享
zhch7:建议9✌️把学历加黑加粗,如果实在offer可能是觉得佬不会去
投了多少份简历才上岸
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-03 18:22
投了几百份简历,专业和方向完全对口,都已读不回。尝试改了一下学校,果然有奇效。
steelhead:这不是很正常嘛,BOSS好的是即便是你学院本可能都会和聊几句,牛客上学院本机会很少了
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务