首页 > 其他 > 详细

Leetcode 257. Binary Tree Paths

时间:2020-02-07 11:13:34      阅读:58      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 cpp代码,easy的,寻找路径,只需要传递string,然后在叶子节点进行加入vector即可。

/**
 * 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> v;
        getPath(true, v, root, "");
        return v;
    }
    void getPath(bool start, vector<string>& v, TreeNode* root, string str) {
        if(!root) return;
        if(!start) str += "->"; 
        str += to_string(root->val);
        if(!root->left && !root->right) v.push_back(str);
        getPath(false, v, root->left, str);
        getPath(false, v, root->right, str);
    }
};

 

Leetcode 257. Binary Tree Paths

原文:https://www.cnblogs.com/littlepage/p/12271838.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!