257. 二叉树的所有路径

给定一个二叉树,返回所有从根节点到叶子节点的路径。
先序遍历

void iterator(TreeNode* root,vector &res,string s){           
            if(s.empty()) s += to_string(root->val);     
            else s+="->" + to_string(root->val);
            if (root->left == NULL && root->right == NULL)            
                res.push_back(s);         
            if (root->left != NULL)            
                iterator(root->left,res,s);         
            if (root->right != NULL)            
                iterator(root->right,res,s);     
        }
    vector binaryTreePaths(TreeNode* root) {
        vector res;
        if(root == NULL) return res;
        string s;
        iterator(root, res, s);
        return res;
    }

你可能感兴趣的:(257. 二叉树的所有路径)