c# dfs solution


  • 0
    N
    public class Solution {
        List<IList<int>> result = new List<IList<int>>();
        public IList<IList<int>> PathSum(TreeNode root, int sum) {
            dfs(root, sum, new List<int>());
            return result;
        }
        
        public void dfs(TreeNode node, int target, IList<int> list) {
            if(node == null) return;
    
            var l = new List<int>(list);
            l.Add(node.val);
            
            if(node.left ==null && node.right ==null && node.val == target){
                result.Add(new List<int>(l));
            }
            
            if(node.left!=null) dfs(node.left, target - node.val, l);
            if(node.right!=null) dfs(node.right, target - node.val, l);
        }
    }
    

Log in to reply
 

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