DFS with StringBuilder


  • 0
    A
    public class Solution {
        public List<String> binaryTreePaths(TreeNode root) {
            List<String> res = new LinkedList<String>();
            if(root != null)    DFS(root,new StringBuilder(),res);
            return res;
        }
        
        public void DFS(TreeNode root, StringBuilder sb, List<String> res) {
            sb.append(root.val);
            if( root.left == null && root.right == null ) res.add(sb.toString());
            sb.append("->");
            if( root.left != null)  DFS(root.left,new StringBuilder(sb),res);
            if( root.right != null) DFS(root.right,new StringBuilder(sb),res);
        }
    }
    

  • 0

    Lao biao wen 666


  • 0
    A

    @ChangLiuCCCCCC 777


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.