LeetCode 257 Binary Tree Paths (DFS)
来源:互联网 发布:星星知我心幼年弯弯 编辑:程序博客网 时间:2024/05/16 08:23
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"]
题目分析:DFS即可 (16ms 击败76%)
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public void DFS(TreeNode root, List<String> ans, String cur) { if (root.left == null && root.right == null) { ans.add(cur); return; } if (root.left != null) { DFS(root.left, ans, cur + "->" + root.left.val); } if (root.right != null) { DFS(root.right, ans, cur + "->" + root.right.val); } } public List<String> binaryTreePaths(TreeNode root) { List<String> ans = new ArrayList<>(); if (root == null) { return ans; } DFS(root, ans, root.val + ""); return ans; }}
阅读全文
0 0
- LeetCode 257 Binary Tree Paths (DFS)
- LeetCode Binary Tree Paths 树 + DFS
- leetcode 257. Binary Tree Paths(DFS)
- leetcode 257: Binary Tree Paths
- leetCode #257 Binary Tree Paths
- LeetCode(257)Binary Tree Paths
- [leetcode 257]Binary Tree Paths
- leetcode[257] Binary Tree Paths
- LeetCode 257: Binary Tree Paths
- leetcode 257 : Binary Tree Paths
- 【LEETCODE】257-Binary Tree Paths
- [leetcode 257] Binary Tree Paths
- LeetCode 257 Binary Tree Paths
- LeetCode 257:Binary Tree Paths
- LeetCode 257:Binary Tree Paths
- leetcode 257:Binary Tree Paths
- leetcode-257-Binary Tree Paths
- LeetCode 257Binary Tree Paths
- 更换openstack虚拟机ip
- 插入数据库遇到“从bcp客户端收到一个对colid x无效的列长度 ”
- spring boot 注册类型转换 (转载)
- linux 服务器部署 lnamp 三、php安装
- C++保留小数点后两位
- LeetCode 257 Binary Tree Paths (DFS)
- 暑期学习 DCGAN 笔记
- greenplum使用postgis例子
- linux环境搭建
- Python发送请求代token
- 中国移动申请FDD牌照加码物联网_你怎么看?
- codility PermCheck
- android RecyclerView 通用适配器
- C# DataGridView控件中点击选中行颜色更改