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->5", "1->3"]

4、解题方法1

本题可以使用深度优先搜索遍历整棵树来解决。

Java代码如下:

import java.util.ArrayList;
import java.util.List;

/**
 * @功能说明:LeetCode 257 - Binary Tree Paths
 * @开发人员:Tsybius2014
 * @开发时间:2015年11月11日
 */
public class Solution {

    //最后返回的List
    private List<String> list;
    
    /**
     * 获取一棵树从顶点到每个叶节点的路径
     * @param root
     * @return
     */
    public List<String> binaryTreePaths(TreeNode root) {
        list = new ArrayList<String>();
        if (root == null) {
            //Do Nothing
        } else if (root.left == null && root.right == null) {
            list.add(String.valueOf(root.val));
        } else {
            if (root.left != null) {
                traverse(root.left, String.valueOf(root.val));
            }
            if (root.right != null) {
                traverse(root.right, String.valueOf(root.val));
            }
        }
        return list;
    }
    
    /**
     * 遍历整棵树
     * @param treeNode
     * @param s
     */
    public void traverse(TreeNode treeNode, String s) {
        if (treeNode.left == null && treeNode.right == null) {
            list.add(s + "->" + treeNode.val);
            return;
        } else {
            if (treeNode.left != null) {
                traverse(treeNode.left, s + "->" + treeNode.val);
            }
            if (treeNode.right != null) {
                traverse(treeNode.right, s + "->" + treeNode.val);
            }
        }
    }
}

END

时间: 2024-10-09 14:50:15

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

LeetCode&mdash;&mdash;Same Tree(判断两棵树是否相同)

问题: Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value.   分析: 考虑使用深度优先遍历的方法,同时遍历两棵树,遇到不等的就返回. 代码如下: /** * Definition f

[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

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

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

[leetcode]Binary Tree Level Order Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal/ 题意:二叉树的层序遍历的实现. 解题思路:二叉树的层序遍历可以用bfs或者dfs来实现.这里使用的dfs实现,代码比较简洁.实际上,二叉树的先序遍历就是dfs实现.   比如一棵树如下: 1 /  \ 2       3 /    \    /   \ 4     5  6    7    二叉树的先序遍历为{1,2,4,5,3,6,7},可以看到这个遍

LeetCode: Binary Tree Maximum Path Sum [124]

[题目] Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. [题意] 给定一棵二叉树,找出其中路径和最大的路径,然会返回最大路径和. 本题中的路径不是从根节点到叶子节点这样的传统的路径,而是指的二叉树中任意两个节点之间的联通路径.