leetcode257. 二叉树的所有路径

给定一个二叉树,返回所有从根节点到叶子节点的路径。

说明: 叶子节点是指没有子节点的节点。

示例:

输入:

   1
 /   \
2     3
 \
  5

输出: ["1->2->5", "1->3"]

解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3

思路:

自己做的比较麻烦,建议直接看后面的

我的思路还是递归,但是首先整数转换字符串,用to_string就好了

to_string(root->val)

我用的

		ostringstream   s1;
		s1<<(root->val);
		string s=s1.str();

比较麻烦,顺便再看看

再来复习下字符串转整数

https://blog.csdn.net/m0_37561165/article/details/81129241

然后就是先把根放进去,然后再递归后面的,其实也没必要。

最后就是为了计算多少个路径,搞得先左边一条,然后右边一条,如果左边没有,右边就不用增加路径数了。。。

老是靠着昨天非递归遍历二叉树的思路再想,其实这个题没这么麻烦。

/**
 * 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 pathout(TreeNode* root,vector<string>& res,int& branch)
    {
        ostringstream   s1;
        if(root==NULL)
        {
            return;
        }
        else
        {
            s1<<root->val;
            string s=s1.str();
            res[branch].append("->");
            res[branch].append(s);
            string mains=res[branch];
            pathout(root->left,res,branch);
            if (root->right)
            {		
                if (root->left){
                    res.push_back(mains);
                    branch++;
                }
                pathout(root->right,res,branch);
            }

        }
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> res;
        if(root==NULL)
            return res;
        else{
            ostringstream   s1;
            s1<<(root->val);
            string s=s1.str();

            int branch=0;
            res.push_back(s);
            pathout(root->left,res,branch);
            if (root->right)
            {
                if(root->left){
                    res.push_back(s);
                    branch++;
                }
                pathout(root->right,res,branch);	
            }
            return res;
        }
    }
};

在库文件string中,to_string(int value)是把一个整数转换为字符串;

人家的思路比较好:

根据叶节点判断路径的数量

https://www.cnblogs.com/Allen-rg/p/7064985.html

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
class Solution {
public:
    /**
     * @param root the root of the binary tree
     * @return all root-to-leaf paths
     */
      
    /*
    分析:一般二叉树的问题用递归解决比较简洁。这里其实是先序遍历;
    在库文件string中,to_string(int value)是把一个整数转换为字符串;
    两个字符串使用“+”连接,是字符串的无空格连接,这点需要了解下面给出代码:
    */
    vector<string> binaryTreePaths(TreeNode* root) {
        // Write your code here
        vector<string> res;
        if(root==NULL) return res;
        binaryTreePathsCore(root,res,to_string(root->val));
        return res;
    }
     
    void binaryTreePathsCore(TreeNode* root,vector<string> &str,string strpath){
         
        if(root->left==NULL&&root->right==NULL){
            //叶子结点
            str.push_back(strpath);
            return;
        }
        if(root->left!=NULL){
            binaryTreePathsCore(root->left,str,strpath+"->"+to_string(root->left->val));
        }
        if(root->right!=NULL){
            binaryTreePathsCore(root->right,str,strpath+"->"+to_string(root->right->val));
        }
    }
};

猜你喜欢

转载自blog.csdn.net/m0_37561165/article/details/81129065
今日推荐