leetcode--257.Binary Tree Paths

使用深度优先搜索的方法遍历每一条枝,遍历完的终止条件为左右子树都为空,这时候将这条枝的string 插入vector 中。

class Solution {
public:
// vector 一直插入string,所以为引用类型,string不能是引用类型
    void dfs(TreeNode *root, vector<string> & result, string temp)
    {
        if(root->left==NULL&&root->right==NULL)
            result.push_back(temp);
        if(root->left)
            dfs(root->left,result,temp+"->"+to_string(root->left->val));
        if(root->right)
            dfs(root->right,result,temp+"->"+to_string(root->right->val));
    }

    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string>result;
        if(root==NULL)
            return result; 

        string temp=to_string(root->val);
        dfs(root, result, temp);

        return result;

    }
};

  第二种方法,在函数内部进行递归:

vector<string> binaryTreePaths(TreeNode *root)
    {
        vector<string> result;

        if(root==NULL)
            return result;

        if(root->left==NULL&& root->right==NULL)
            return {to_string(root->val)};

        vector<string> left=binaryTreePaths(root->left);
        vector<string> right=binaryTreePaths(root->right);

        left.insert(left.end(),right.begin(),right.end());
        vector<string>::iterator iter;
        for(iter=left.begin();iter!=left.end();iter++)
        {
            *iter=to_string(root->val)+"->"+*iter;
        }
        return left;
    }

  

时间: 2024-08-08 11:32:04

leetcode--257.Binary Tree Paths的相关文章

leetCode 257. Binary Tree Paths 二叉树路径

257. Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree:    1  /   2     3    5 All root-to-leaf paths are: ["1->2->5", "1->3"] 思路: 1.采用二叉树的后序遍历非递归版 2.在叶子节点的时候处理字

LeetCode 257. Binary Tree Paths (二叉树路径)

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 题目标签:Tree 这道题目给了我们一个二叉树,让我们记录所有的路径,返回一个array string list. 我们可以另外设一个fin

Java [Leetcode 257]Binary Tree Paths

题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 解题思路: 使用递归法. 代码如下: /** * Definition for a binary tree node. * pu

(easy)LeetCode 257.Binary Tree Paths

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 思想:递归代码如下: /** * Definition for a binary tree node. * public class Tre

Leetcode 257 Binary Tree Paths 二叉树 DFS

找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 p

LeetCode:Binary Tree Paths - 获取一棵树从顶点到每个叶节点的路径

1.题目名称 Binary Tree Paths(获取一棵树从顶点到每个叶节点的路径) 2.题目地址 https://leetcode.com/problems/binary-tree-paths/ 3.题目内容 英文:Given a binary tree, return all root-to-leaf paths. 中文:给定一颗二叉树,返回所有的根节点到叶节点的路径 例如:现有一颗二叉树    1  /   2     3    5 所有由根节点到叶节点的路径如下: ["1->2-

&lt;LeetCode OJ&gt; 257. Binary Tree Paths

257. Binary Tree Paths My Submissions Question Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["

[LeetCode][JavaScript]Binary Tree Paths

Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] https://leetcode.com/problems/binary-tree-paths/ 树的遍

【LeetCode】257. Binary Tree Paths 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51474128 Subject 出处:https://leetcode.com/problems/binary-tree-paths/ Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-le

【LeetCode】257 - Binary Tree Paths

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] Solution: /** * Definition for a binary tree node. * struct TreeNode {