首页 > 其他 > 详细

Binary Tree Paths

时间:2015-12-08 17:58:59      阅读:271      评论:0      收藏:0      [点我收藏+]

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:

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

  

Binary Tree Paths

原文:http://www.cnblogs.com/aguai1992/p/5029487.html

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