easy257. Binary Tree Paths 二叉树找到所有路径

Posted Sherry_Yang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了easy257. Binary Tree Paths 二叉树找到所有路径相关的知识,希望对你有一定的参考价值。

http://blog.csdn.net/crazy1235/article/details/51474128

花样做二叉树的题……居然还是不会么……

 

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

 

以上是关于easy257. Binary Tree Paths 二叉树找到所有路径的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 257. Binary Tree Paths_ Easy tag: DFS

257. Binary Tree Paths

*lintcode246- Binary Tree Path Sum II- easy

Lintcode376-Binary Tree Path Sum-Easy

257. Binary Tree Paths

257. Binary Tree Paths 257.二叉树路径