获得二叉树的所有路径,代码非常简洁。
257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1 |
1 |
All root-to-leaf paths are:
1 |
["1->2->5", "1->3"] |
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
算法:基于深度优先遍历算法,遇到叶子节点就将当前路径加入结果集合。
1 |
public List<String> (TreeNode root) { |
近期评论