257. Binary Tree Paths

String可以直接加int

 

 1 class Solution {
 2     List<String> res = new ArrayList<>();
 3     public List<String> binaryTreePaths(TreeNode root) {
 4         if(root == null) return res;
 5         backtrack(root, "");
 6         return res;
 7         
 8     }
 9     
10     public void backtrack(TreeNode root, String str){
11         if(root.left == null && root.right == null){
12             str = str + root.val;
13             res.add(str);
14             return;
15         }
16         if(root.left != null){ //注意要不等于才可以
17             backtrack(root.left, str+ root.val + "->");
18         }
19         if(root.right != null){
20              backtrack(root.right, str + root.val + "->");
21         }
22        
23     }
24 }

 

posted @ 2018-10-26 06:21  jasoncool1  阅读(72)  评论(0编辑  收藏  举报