- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Good Nodes in Binary Tree in C++
Suppose we have a binary tree, a node X in the tree is named good when in the path from root to X there are no nodes whose value is greater than X. Here we have to find the number of good nodes in the binary tree.
So, if the input is like,
then the output will be 4, the colored nodes are good node.
To solve this, we will follow these steps −
Define a function dfs(), this will take node, val,
if node is null, then −
return
ret := ret + (1 when val <= val of node, otherwise 0)
dfs(left of node, maximum of val and val of node)
dfs(right of node, maximum of val and val of node)
From the main method do the following −
ret := 0
dfs(root, -inf)
return ret
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class TreeNode{ public: int val; TreeNode *left, *right; TreeNode(int data){ val = data; left = NULL; right = NULL; } }; void insert(TreeNode **root, int val){ queue<TreeNode*> q; q.push(*root); while(q.size()){ TreeNode *temp = q.front(); q.pop(); if(!temp->left){ if(val != NULL) temp->left = new TreeNode(val); else temp->left = new TreeNode(0); return; }else{ q.push(temp->left); } if(!temp->right){ if(val != NULL) temp->right = new TreeNode(val); else temp->right = new TreeNode(0); return; }else{ q.push(temp->right); } } } TreeNode *make_tree(vector<int> v){ TreeNode *root = new TreeNode(v[0]); for(int i = 1; i<v.size(); i++){ insert(&root, v[i]); } return root; } class Solution { public: int ret; void dfs(TreeNode* node, int val){ if (!node) return; ret += val <= node->val; dfs(node->left, max(val, node->val)); dfs(node->right, max(val, node->val)); } int goodNodes(TreeNode* root){ ret = 0; dfs(root, INT_MIN); return ret; } }; main(){ Solution ob; vector<int> v = {3,1,4,3,NULL,1,5}; TreeNode *root = make_tree(v); cout << (ob.goodNodes(root)); }
Input
{3,1,4,3,NULL,1,5}
Output
4
- Related Articles
- Count Non-Leaf nodes in a Binary Tree in C++
- Count half nodes in a Binary tree (Iterative and Recursive) in C++
- Count full nodes in a Binary tree (Iterative and Recursive) in C++
- Validate Binary Tree Nodes in C++
- Count Complete Tree Nodes in C++
- All Nodes Distance K in Binary Tree in C++
- C++ Pairwise Swap Leaf Nodes in a Binary Tree
- Print all full nodes in a Binary Tree in C++
- Product of all nodes in a Binary Tree in C++
- Print all odd nodes of Binary Search Tree in C++
- Print all internal nodes of a Binary tree in C++
- Print all even nodes of Binary Search Tree in C++
- Product of all leaf nodes of binary tree in C++
- Find maximum among all right nodes in Binary Tree in C++
- Find distance between two nodes of a Binary Tree in C++

Advertisements