<LeetCode OJ> 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:

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

Credits:

Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Hide Tags

Tree Depth-first
Search

Hide Similar Problems

(M) Path Sum II

/**
 * 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 getDfsPaths(vector<string>& result, TreeNode* node, string strpath) {
        if(!node->left && !node->right){//叶子
            result.push_back(strpath);
            return ;
        }
        if(node->left)
            getDfsPaths(result, node->left, strpath+"->"+to_string(node->left->val));
        if(node->right)
            getDfsPaths(result, node->right, strpath+"->"+to_string(node->right->val));
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> ret;
        if(!root)
            return ret;

        getDfsPaths(ret, root, to_string(root->val));
        return ret;
    }
};

注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50493936

原作者博客:http://blog.csdn.net/ebowtang

时间: 2024-08-02 11:04:45

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

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

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

重做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"] Solution1: 用string是最方便的.因为string是immutable的,所以当返回的时候不会带回最后加的string. /*

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"] 用backtracking的方法.与113题很类似.需要注意其实root元素 public IList<string> Bina

257. Binary Tree Paths java solutions

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"] Credits:Special thanks to @jianchao.li.fighter for adding this problem

257. Binary Tree Paths [easy] (Python)

题目链接 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-leaf paths are: ["1->2->5", "1->3"] 题目翻译 给定一个二叉

【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

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. # class TreeNode(object): # def _