Day i do not know
This commit is contained in:
11
112. Path Sum/112. Path Sum.csproj
Normal file
11
112. Path Sum/112. Path Sum.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_112._Path_Sum</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
9
112. Path Sum/Program.cs
Normal file
9
112. Path Sum/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace _112._Path_Sum;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
}
|
||||
}
|
||||
21
112. Path Sum/Solution.cs
Normal file
21
112. Path Sum/Solution.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
namespace _112._Path_Sum;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public bool HasPathSum(TreeNode root, int targetSum)
|
||||
=> root != null ? Recursion(root, root.val, targetSum) : false;
|
||||
|
||||
private bool Recursion(TreeNode? node, int curSum, int targetSum)
|
||||
{
|
||||
if (curSum == targetSum && node != null && node.left == null && node.right == null)
|
||||
return true;
|
||||
if (node == null)
|
||||
return false;
|
||||
bool res = false;
|
||||
if(node.left != null)
|
||||
res |= Recursion(node.left, curSum + node.left.val, targetSum);
|
||||
if (node.right != null)
|
||||
res |= Recursion(node.right, curSum + node.right.val, targetSum);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
16
112. Path Sum/TreeNode.cs
Normal file
16
112. Path Sum/TreeNode.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace _112._Path_Sum;
|
||||
|
||||
|
||||
//Definition for a binary tree node.
|
||||
public class TreeNode
|
||||
{
|
||||
public int val;
|
||||
public TreeNode? left;
|
||||
public TreeNode? right;
|
||||
public TreeNode(int val = 0, TreeNode? left = null, TreeNode? right = null)
|
||||
{
|
||||
this.val = val;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user