257. Binary Tree Paths
来源:互联网 发布:linux 守护进程脚本 编辑:程序博客网 时间:2024/05/23 01:15
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
dfs
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { private List <String> ret = new ArrayList <String> (); private void depDFS (String s,TreeNode root ){ if(root.left==null&&root.right==null)ret.add(s+"->"+String.valueOf(root.val)); if(root.left!=null)depDFS(s+"->"+String.valueOf(root.val),root.left); if(root.right!=null)depDFS(s+"->"+String.valueOf(root.val),root.right); } public List<String> binaryTreePaths(TreeNode root) { if(root==null)return ret; String s = String.valueOf(root.val); if(root.left==null&&root.right==null)ret.add(s); if(root.left!=null)depDFS(s,root.left); if(root.right!=null)depDFS(s,root.right); return ret; }}
0 0
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- B树、B-树、B+树、B*树 红黑树
- OpenLayers 3 之 事件体系详解
- 页面生成树形查询的后台实现方法
- zjnu MAFIJA (图论)
- POJ 2098 数值积分
- 257. Binary Tree Paths
- 7. Java 注释类型
- Spring学习笔记(1-2)
- gan
- 第六届蓝桥杯校内选拔
- 使用HOOK 拦截 WM_DEVICECHANGE消息
- asp循环遍历数据库ACCESS
- visual studio 2013上搭建arm交叉编译环境(一) 引言
- 蓝桥杯 2015校内选拔试题 第六题:1/a 的分数称为单位分数