LeetCode 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> svec;
		string str;
		if (root == NULL)return svec;
		dfs(root, str, svec);
		return svec;
	}

	void dfs(TreeNode *root, string str, vector<string> &svec)
	{
	    if(root==nullptr)  return;
		if (root->left == NULL&&root->right == NULL)
		{
		    str+=to_string(root->val);
			svec.push_back(str); //如果遍历到叶子节点,则将这个路径放到容器中去
			return;
		}
		dfs(root->left,  str+to_string(root->val)+"->", svec);//遍历左子树
		dfs(root->right, str+to_string(root->val)+"->", svec);//遍历右子树

	}

};

  

时间: 2024-10-13 16:27:24

LeetCode Binary Tree Paths的相关文章

[LeetCode] 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 Binary Tree Paths(简单题)

题意: 给出一个二叉树,输出根到所有叶子节点的路径. 思路: 直接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 */

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-

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

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

[LintCode] 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"] LeetCode上的原题,请参见我之前的博客Binary Tree Paths. 解法一: class Solution {

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 Zigzag Level Order Traversal

Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#