public static IList <IList <int> > PathSum(TreeNodePS root, int sum) { IList <IList <int> > result = new List <IList <int> > (); PathSumUtil(root, sum, result, new List <int> (), null); return(result); }
private static void PathSumUtil(TreeNodePS node, int sum, IList <IList <int> > result, List <int> tempList, TreeNodePS currParent) { if (node == null) { return; } sum -= node.val; tempList.Add(node.val); if (sum == 0 && node.left == null && node.right == null) { result.Add(new List <int> (tempList)); } PathSumUtil(node.left, sum, result, tempList, node); PathSumUtil(node.right, sum, result, tempList, node); tempList.RemoveAt(tempList.Count - 1); }
public TreeNodePS(int val = 0, TreeNodePS left = null, TreeNodePS right = null) { this.val = val; this.left = left; this.right = right; }