[leetcode]144. Binary Tree Preorder Traversal

[leetcode]144. Binary Tree Preorder Traversal


Analysis

明天就是中秋节了,那就提前拜个早年吧—— [ummm~]

Given a binary tree, return the preorder traversal of its nodes’ values.
前序遍历二叉树,然后输出节点值。

Implement

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        helper(root);
        return res;
    }
    void helper(TreeNode* root){
        if(!root)
            return ;
        res.push_back(root->val);
        helper(root->left);
        helper(root->right);
    }
private:
    vector<int> res;
};

猜你喜欢

转载自blog.csdn.net/weixin_32135877/article/details/82825272