[LeetCode257]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.
* struct TreeNode {
*     int val;(
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
    void RootPath(vector<string>& resultVec, TreeNode *root, string s)
    {
        if (root->left == NULL && root->right == NULL)
        {
            resultVec.push_back(s);
            return;
        }
        if (root->left)
        {
            RootPath(resultVec, root->left, s + "->" + to_string(root->left->val));
        }
        if (root->right)
        {
            RootPath(resultVec, root->right, s + "->" + to_string(root->right->val));
        }
    }

    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        if (root == NULL) return result;
        RootPath(result, root, to_string(root->val));
        return result;
    }
};
时间: 2024-10-18 08:15:09

[LeetCode257]Binary Tree Paths的相关文章

LeetCode257——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. * struct TreeNode { *    

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==NU

Binary Tree Paths

输出二叉树的寻叶路径Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1  /   \2    3  \   5All root-to-leaf paths are: ["1->2->5", "1->3"] 遍历二叉树的时候,每当寻到叶结点,把路径装进结果里 1 package com.rust.data

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/ 树的遍

[lintcode easy]Binary Tree Paths

Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Example Given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: [ "1->2->5", "1->3" ] /** * Definition of TreeNode: * public class TreeNode {

LintCode Binary Tree Paths

Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tree: 1 1 2 / 3 2 3 4 5 5 All root-to-leaf paths are: 1 [ 2 "1->2->5", 3 "1->3" 4 ] As mentioned in the problem we want to get al

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.在叶子节点的时候处理字