Professional Documents
Culture Documents
Lab Task #09
Lab Task #09
8]
[LAB #09]
Binary Trees
TASK 1:Write a method for finding the sum of all elements in a binary
tree.
SOLUTION:
OUTPUT
TASK 2:Write a method for checking the existence of path with given sum. That means, given a sum,
check whether there exist a path from root to any of the nodes.
SOLUTION:
1
CT-[Roll No.]CT-79 Data Structures Algorithms and ApplicationsLab Session: [No.8]
OUTPUT
1
CT-[Roll No.]CT-79 Data Structures Algorithms and ApplicationsLab Session: [No.8]
TASK 3: Write a method for finding the number of half nodes (nodes with only one child) in the
binary tree.
SOLUTION:
OUTPUT
Task 1 lab 9
#include <iostream>
using namespace std;
struct Node {
1
CT-[Roll No.]CT-79 Data Structures Algorithms and ApplicationsLab Session: [No.8]
int val;
Node* left;
Node* right;
};
int main() {
return 0;
}
Output:
1
CT-[Roll No.]CT-79 Data Structures Algorithms and ApplicationsLab Session: [No.8]
#include <iostream>
using namespace std;
struct Node {
int val;
Node* left;
Node* right;
};
int main() {
1
CT-[Roll No.]CT-79 Data Structures Algorithms and ApplicationsLab Session: [No.8]
postorder_traversal(root);
cout << endl;
return 0;
}
1
CT-[Roll No.]CT-79 Data Structures Algorithms and ApplicationsLab Session: [No.8]
1
CT-[Roll No.]CT-79 Data Structures Algorithms and ApplicationsLab Session: [No.8]