diff --git a/DSA Problems 7 Binary Tree/Validate_binary_tree_nodes.cpp b/DSA Problems 7 Binary Tree/Validate_binary_tree_nodes.cpp new file mode 100644 index 0000000..ce29717 --- /dev/null +++ b/DSA Problems 7 Binary Tree/Validate_binary_tree_nodes.cpp @@ -0,0 +1,48 @@ +/* +1361. Validate Binary Tree Nodes + +Problem Link:- https://leetcode.com/problems/validate-binary-tree-nodes/?envType=daily-question&envId=2023-10-17 + +You have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i], return true if and only if all the given nodes form exactly one valid binary tree. + +If node i has no left child then leftChild[i] will equal -1, similarly for the right child. + +Note that the nodes have no values and that we only use the node numbers in this problem. + +Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1] +Output: false + +*/ + +// SOLUTION FUNCTION +class Solution { +public: + bool validateBinaryTreeNodes(int n, vector& leftChild, vector& rightChild) { + vectorindegree(n,0); + for(int i=0;ivisited(n,false); + queueq; + q.push(root); + while(!q.empty()){ + int node=q.front(); + q.pop(); + if(visited[node]) return false; + visited[node]=true; + if(leftChild[node]!=-1) q.push(leftChild[node]); + if(rightChild[node]!=-1) q.push(rightChild[node]); + } + return accumulate(visited.begin(),visited.end(),0)==n; + } +}; \ No newline at end of file