首页 > 其他 > 详细

【leetcode】 257. Binary Tree Path

时间:2015-08-21 17:09:28      阅读:237      评论:0      收藏:0      [点我收藏+]
/**
 * @author        johnsondu
 * @time          2015.8.21 16:30
 * @description   tranverse a tree
 * @url           https://leetcode.com/problems/binary-tree-paths/
 */ 

/**
 * 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:
    void tranverse(TreeNode *child, string str, vector<string> &vec) {
        if(str == "") str += to_string(child->val);
        else str += ("->" + to_string(child->val));
        
        if(!child->left && !child->right) {
            vec.push_back(str);
            return;
        }
        
        if(child->left) tranverse(child->left, str, vec);
        if(child->right) tranverse(child->right, str, vec);
    }
    
    vector<string> binaryTreePaths(TreeNode* root) {
        TreeNode *head = root;
        vector<string> ans;
        if(!head) return ans;
        
        string cnt("");
        tranverse(head, cnt, ans);
        return ans;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

【leetcode】 257. Binary Tree Path

原文:http://blog.csdn.net/zone_programming/article/details/47836637

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