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:

vector<string> binaryTreePaths(TreeNode* root) {

vector<string> vs;

if (root == NULL) return vs;

if (root->left == NULL && root->right == NULL) {

char buff[124];

sprintf(buff, "%d", root->val);

vs.push_back(buff);

return vs;

}

if (root->left) {

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

for (int i = 0; i < lefts.size(); i++) {

char *buff = new char[lefts[i].size()+56];

sprintf(buff, "%d->%s", root->val, lefts[i].c_str());

vs.push_back(buff);

delete[] buff;

}

}

if (root->right) {

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

for (int i = 0; i < rights.size(); i++) {

char *buff = new char[rights[i].size()+56];

sprintf(buff, "%d->%s", root->val, rights[i].c_str());

vs.push_back(buff);

delete[] buff;

}

}

return vs;

}

};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-02 17:59:26

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

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