public static IList <IList <int> > PathSum(LeetCode113TreeNode root, int sum) { var pathNodes = new List <int>(); var pathsList = new List <IList <int> >(); Helper(root, sum, pathNodes, pathsList); return(pathsList); }
private static void Helper(LeetCode113TreeNode root, int sum, List <int> pathNodes, List <IList <int> > pathsList) { if (root == null) { return; } pathNodes.Add(root.val); if (sum == root.val && root.IsLeaf()) { var temp = new List <int>(pathNodes); pathsList.Add(temp); } else { Helper(root.left, sum - root.val, pathNodes, pathsList); Helper(root.right, sum - root.val, pathNodes, pathsList); } pathNodes.RemoveAt(pathNodes.Count - 1); }
public LeetCode113TreeNode(int val = 0, LeetCode113TreeNode left = null, LeetCode113TreeNode right = null) { this.val = val; this.left = left; this.right = right; }