C# Solution


  • 0
    public class Solution {
        public IList<string> BinaryTreePaths(TreeNode root) {
            List<string> result = new List<string>();
            
            if (root != null)
                ExtendPath(root, string.Empty, result);
            
            return result;
        }
        
        private void ExtendPath(TreeNode node, string currentPath, List<string> result)
        {
            currentPath += node.val.ToString() + "->";
            
            if (node.left == null && node.right == null)
                result.Add(currentPath.Substring(0, currentPath.Length - 2));
            
            if (node.left != null)
                ExtendPath(node.left, currentPath, result);
            
            if (node.right != null)
                ExtendPath(node.right, currentPath, result);
        }
    }

Log in to reply
 

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