码迷,mamicode.com
首页 > 其他好文 > 详细

Binary Tree Paths

时间:2015-12-08 18:00:45      阅读:111      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!