예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
 public LeetCode113TreeNode(int val = 0, LeetCode113TreeNode left = null, LeetCode113TreeNode right = null)
 {
     this.val   = val;
     this.left  = left;
     this.right = right;
 }