
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to check whether a binary tree has the given path sum in C#?
HasPathsum takes 2 parameters one is the tree node and other is the sum value, initially we check whether the node is null or not, if the node is null then we return false. If the node is not null then we call HasPathSum recursive method, in each and every recursive step we keep on subtracting the sum value from the node value. If the value of the sum reaches 0 then we come to conclusion that the given tree has the path that is equal to sum and return true.
Example
public class TreesPgm{ public class Node{ public int Value; public Node LeftChild; public Node RightChild; public Node(int value){ this.Value = value; } public override String ToString(){ return "Node=" + Value; } } public bool HasPathSum(Node node, int sum){ if (root == null){ return false; } return helperHasPathSum(node, sum); } private bool helperHasPathSum(Node root, int sum){ if (root == null){ return false; } sum -= root.Value; if (root.LeftChild == null && root.RightChild == null && sum == 0){ return true; } return helperHasPathSum(root.LeftChild, sum) || helperHasPathSum(root.RightChild, sum); } }
Input
5 2 6 1 3 7
Output
True
- Related Articles
- C++ Program to Check Whether a Given Tree is Binary Search Tree
- C++ Program to Check Whether a given Binary Tree is a Full Binary Tree or not
- Binary Tree Maximum Path Sum in Python
- Maximum Path Sum in a Binary Tree in C++
- How to check whether a binary tree is a valid binary search tree using recursion in C#?
- C++ Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
- Program to find largest sum of any path of a binary tree in Python
- Program to find sum each of the diagonal path elements in a binary tree in Python
- Program to check whether a binary tree is complete or not in Python
- Program to check whether a binary tree is BST or not in Python
- Program to find sum of longest sum path from root to leaf of a binary tree in Python
- Check if a given Binary Tree is SumTree in C++
- Check if a given Binary Tree is Heap in Python
- Check if a given Binary Tree is Heap in C++
- Check for Children Sum Property in a Binary Tree in C++

Advertisements