[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 {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root the root of the binary tree
     * @return all root-to-leaf paths
     */
     List<String> list=new ArrayList<String>();
    public List<String> binaryTreePaths(TreeNode root) {
        // Write your code here

       if(root!=null)
       {
           getPaths(root,String.valueOf(root.val));
       }

       return list;
    }

    public void getPaths(TreeNode n, String path)
    {
        if(n.left==null && n.right==null)
        {
            list.add(path);
        }

        if(n.left!=null)
        {
            getPaths(n.left,path+"->"+n.left.val);
        }

        if(n.right!=null)
        {
            getPaths(n.right,path+"->"+n.right.val);
        }
    }
}
 
时间: 2024-10-16 13:03:44

[lintcode easy]Binary Tree Paths的相关文章

[lintcode easy]Binary Tree Postorder Traversal

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Challenge Can you do it without recursion? Among preorder,inorder and postorder binary tree

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

[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 {

&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: ["

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-

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

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

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"] 题目翻译 给定一个二叉