[LeetCode] Algorithms-257. Binary Tree Paths

来源:互联网 发布:中农数据食堂采配平台 编辑:程序博客网 时间:2024/06/10 14:27

描述

思路

代码

/** * 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<string> binaryTreePaths(TreeNode* root) {        vector<string> r;        if(root == NULL) return r;        string tem;        tem = to_string(root->val);        searchTree(root, r, tem);        return r;    }    void searchTree(TreeNode* root, vector<string> &r, string tem) {        if(root->left == NULL && root->right == NULL)  r.push_back(tem);        if(root->left != NULL) searchTree(root->left, r, tem + "->" + to_string(root->left->val));        if(root->right != NULL) searchTree(root->right, r, tem + "->" + to_string(root->right->val));    }};