题目描述

257. Binary Tree Paths

方法思路

class Solution {
    //Runtime: 6 ms, faster than 100.00%
    //Memory Usage: 38.7 MB, less than 18.78% 
    public List<String> binaryTreePaths(TreeNode root){
        List<String> res = new ArrayList<>();
        if(root != null) dfs(root, "", res);
        return res;
    }
    public void dfs(TreeNode root, String s, List<String> res){
        if(root.left == null && root.right == null)
            res.add(s + root.val);
        if(root.left != null)
            dfs(root.left, s + root.val + "->", res);
        if(root.right != null)
            dfs(root.right, s + root.val + "->", res);
    }
}

相关文章:

  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-04-10
  • 2021-09-30
  • 2021-08-05
  • 2021-09-22
相关资源
相似解决方案