Binary Tree Path

//Given a binary tree, return all root-to-leaf paths.
//this is not full function.//check again

public class solution{
    class Treednode{
        public Treednode left, right;
        public int val;
        public Treednode(int val){
            this.val = val;
            this.left = null;
            this.right = null;
        }
    } 

    public List<String> binaryTreePaths(TreeNode root) {
        // write your code her

        List<String> res = new ArrayList<>();
         if(root == null) return res;
        helper(res,String.valueOf(root.val),root);

        return res;


    }


    public void helper(List<String> res, String s, TreeNode root){
        if(root == null){
            return;
        }

        if(root.left == null && root.right == null){
            res.add(s);
            return;
        }

        if(root.left!=null){
            helper(res,s+"->"+String.valueOf(root.left.val),root.left);
        }

        if(root.right != null){
            helper(res,s+"->"+String.valueOf(root.right.val),root.right);
        }
    }
}

Last updated