
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Delete Tree Nodes in C++
Suppose we have a tree, this tree is rooted at node 0, this is given as follows −
- Number of nodes is nodes
- Value of ith node is value[i]
- Parent of ith node is parent[i]
We have to remove every subtree whose sum of values of nodes is 0, after doing that return the number of nodes remaining in the tree. So if the tree is like −
There are 7 nodes, the output will be 2
To solve this, we will follow these steps −
- Create a map called children
- define a method called dfs(), this will take node, an array value and graph
- temp := a pair (value[node], 1)
- for i in range 0 to size of graph[node]
- temp2 := dfs(graph[node, i], value, graph)
- increase first by first of temp2, increase second by second of temp2
- if first of temp is 0, then ans := ans – second of temp, set second of temp := 0
- return temp
- From the main method, it will take nodes, parents array and values array
- n := number of values present in values array
- ans := n
- define an array graph of size n + 1
- for i in range 1 to n – 1
- insert i into graph[parent[i]]
- dfs(0, value, graph)
- return ans
Example(C++)
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: map <int, int> children; int ans; pair <int, int> dfs(int node, vector<int>& value, vector <int> graph[]){ pair <int, int> temp = {value[node], 1}; for(int i = 0; i < graph[node].size(); i++){ pair <int, int> temp2 = dfs(graph[node][i], value, graph); temp.first += temp2.first; temp.second += temp2.second; } if(temp.first == 0){ ans -= temp.second; temp.second = 0; } return temp; } int deleteTreeNodes(int nodes, vector<int>& parent, vector<int>& value) { int n = value.size(); ans = n; children.clear(); vector < int > graph[n + 1]; for(int i = 1; i < n; i++){ graph[parent[i]].push_back(i); } dfs(0, value, graph); return ans; } }; main(){ vector<int> v1 = {-1,0,0,1,2,2,2}; vector<int> v2 = {1,-2,4,0,-2,-1,-1}; Solution ob; cout << (ob.deleteTreeNodes(7,v1, v2)); }
Input
7 [-1,0,0,1,2,2,2] [1,-2,4,0,-2,-1,-1]
Output
2
- Related Articles
- Validate Binary Tree Nodes in C++
- Count Complete Tree Nodes in C++
- Delete N nodes after M nodes of a linked list in C++?
- Delete leaf nodes with value k in C++?
- Count Good Nodes in Binary Tree in C++
- Delete N nodes after M nodes of a linked list in C++ program
- Delete alternate nodes of a Linked List in C++
- Delete leaf nodes with value as x in C++?
- Delete leaf nodes with value k in C++ program
- Binary Search Tree - Delete Operation in C++
- All Nodes Distance K in Binary Tree in C++
- C++ Pairwise Swap Leaf Nodes in a Binary Tree
- Delete leaf nodes with value as x in C++ Program
- Print all full nodes in a Binary Tree in C++
- Product of all nodes in a Binary Tree in C++

Advertisements