C# recursive solution


  • 0
    K
    public class Solution {
        List<string> list = new List<string>();
        public IList<string> BinaryTreePaths(TreeNode root) {
    
            string s = String.Empty;
            PrintPath(root,s);
            return list;
        }
        public void PrintPath(TreeNode root, string s)
        {
            if (root==null) return;
    
            if (root.left == null && root.right == null)
            {
                s = s+root.val.ToString();
                list.Add(s);
            }
            else
            {
                s = s+root.val.ToString()+ "->";
            }
    
            PrintPath(root.left,s);
            PrintPath(root.right,s);
        }
    }
    

Log in to reply
 

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