二叉树的所有路径

题目:

给一棵二叉树,找出从根节点到叶子节点的所有路径。

样例

给出下面这棵二叉树:

   1
 /   \
2     3
 \
  5

所有根到叶子的路径为:

[
  "1->2->5",
  "1->3"
]

思路:

这个题使用了引用,方便了很多,这道题也又写了一个函数,all(root,ss,to_string(root->val)); 在函数里使用了递归。

代码:

/**
 * 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
     */
    vector<string> binaryTreePaths(TreeNode* root) {
        // Write your code here
        vector<string> ss; 
        if(root==NULL) return ss; 
        all(root,ss,to_string(root->val)); 
        return ss; 
    } 
    void all(TreeNode *root,vector<string>&str,string strPaths){ 
        if(root->left==NULL&&root->right==NULL){ 
            str.push_back(strPaths); 
            return; 
        } 
        if(root->left!=NULL) 
        all(root->left,str,strPaths+"->"+to_string(root->left->val)); 
        if(root->right!=NULL) 
        all(root->right,str,strPaths+"->"+to_string(root->right->val)); 
    } 
        /*int top=-1;
        //stack<int>st;
        vector<string>aa;
        vector<int>ss;
        TreeNode *s[100];
        string h,k;
       while(root!=NULL||top!=-1)
       {
           while(root!=NULL)
           {
               //cout<<root->val;
               ss.push_back(root->val);
               //st.push(root->val)
               s[++top]=root;
               root=root->left;
           }
           h=to_string(s[0]->val);
           for(int i=1;i<=top+1;i++)
           { h=h+"->"+to_string(s[i]->val);
           }
           aa.push_back(h);
           h=k;
           if(top!=-1)
           { root=s[top--];
             root=root->right;
           }
       }
       return aa;
    }*/
};

感想:

我一开始利用了前序遍历非递归算法,写了一个程序,就是下面那个,老是不对,也改不对,就又换了个,确实这个方便了许多。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用递归的方式来实现求解二叉树所有路径的功能。具体实现方法如下: 1. 定义一个函数,输入参数为当前节点指针、当前路径字符串和结果数组。 2. 如果当前节点为空,则直接返回。 3. 将当前节点的值添加到当前路径字符串中。 4. 如果当前节点为叶子节点,则将当前路径字符串添加到结果数组中。 5. 否则,递归遍历当前节点的左子树和右子树,分别调用上述函数。 6. 递归结束后,将当前节点的值从当前路径字符串中删除,以便遍历其它路径。 下面是具体的 C 代码实现: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_PATH_LEN 100 struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; void binaryTreePathsHelper(struct TreeNode* node, char* path, char** result, int* returnSize) { if (node == NULL) { return; } int len = strlen(path); if (len > ) { path[len] = '-'; path[len+1] = '>'; path[len+2] = '\'; } char val_str[10]; sprintf(val_str, "%d", node->val); strcat(path, val_str); if (node->left == NULL && node->right == NULL) { result[*returnSize] = (char*)malloc(sizeof(char) * (strlen(path) + 1)); strcpy(result[*returnSize], path); (*returnSize)++; } else { binaryTreePathsHelper(node->left, path, result, returnSize); binaryTreePathsHelper(node->right, path, result, returnSize); } path[strlen(path) - strlen(val_str) - 2] = '\'; } char ** binaryTreePaths(struct TreeNode* root, int* returnSize) { char** result = (char**)malloc(sizeof(char*) * MAX_PATH_LEN); *returnSize = ; char path[MAX_PATH_LEN]; path[] = '\'; binaryTreePathsHelper(root, path, result, returnSize); return result; } int main() { struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->val = 1; root->left = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->left->val = 2; root->left->left = NULL; root->left->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->left->right->val = 5; root->left->right->left = NULL; root->left->right->right = NULL; root->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->right->val = 3; root->right->left = NULL; root->right->right = NULL; int returnSize; char** result = binaryTreePaths(root, &returnSize); for (int i = ; i < returnSize; i++) { printf("%s\n", result[i]); } return ; } ``` 运行结果如下: ``` 1->2->5 1->3 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值