From e49aea44877d3656cb1522260b858ff90bd4108f Mon Sep 17 00:00:00 2001 From: Aarushi Maurya Date: Sat, 2 Apr 2022 00:30:29 +0530 Subject: [PATCH] chore(CPlusPlus): add print all nodes at a level on trees (#696) Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com> Co-authored-by: Rahul Rajeev Pillai <66192267+Ashborn-SM@users.noreply.github.com> --- algorithms/CPlusPlus/README.md | 1 + .../Trees/print-all-nodes-at-a-level.cpp | 83 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 algorithms/CPlusPlus/Trees/print-all-nodes-at-a-level.cpp diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 73e0442e..c545fa88 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -140,6 +140,7 @@ - [Finding the elements of a tree visible from top view](Trees/Top-View-Of-A-Tree.cpp) - [Binary Tree Implementation](Trees/binary-tree-implementation.cpp) - [Iterative Segment Tree](Trees/IterativeSegmentTree.cpp) +- [Print all nodes at level k](Trees/print-all-nodes-at-a-level.cpp) - [Sum of right leaves](Trees/sum-of-right-leaves.cpp) ## Trie diff --git a/algorithms/CPlusPlus/Trees/print-all-nodes-at-a-level.cpp b/algorithms/CPlusPlus/Trees/print-all-nodes-at-a-level.cpp new file mode 100644 index 00000000..09f35a7a --- /dev/null +++ b/algorithms/CPlusPlus/Trees/print-all-nodes-at-a-level.cpp @@ -0,0 +1,83 @@ +/* Description - We have to print all nodes at a level 'k' of the tree. + +For example- If we are given the following tree, the nodes at level 1 are 1, nodes at level 2 are 2,3, nodes at level 3 are 7, 9 and nodes at level 4 are 21 and 11. +(Note- The level starts from 1, i.e root is at level 1) + + 1 level 1 + / \ + 2 3 level 2 + / \ + 7 9 level 3 + / \ + 21 11 level 4 +*/ + +#include +using namespace std; + +typedef struct Node +{ + int data; + struct Node* left; + struct Node* right; + + Node(int val) + { + data= val; + left=right=NULL; + } +}node; + +//function for level order traversal +void levelorder(Node* root, int k) +{ + if(root==NULL) + return; + + queue q; + + q.push(root); + int count=0; //for calculating at which level we are. + + while(!q.empty()) + { + count++; //increment the value of count as level is incremented + int n=q.size(); + + for(int i=0; idata<<" "; + } + if(temp->left!=NULL){ + q.push(temp->left); + } + if(temp->right!=NULL){ + q.push(temp->right); + } + } + } +} + +//main function +int main() +{ + node*root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->right->left=new node(7); + root->right->right=new node(9); + root->right->left->left=new node(21); + root->right->right->right=new node(11); + + int k = 3; // here we have taken k=3 (third level of tree) + + levelorder(root,k); //calling level order function +} + +// The output of the above program will be 7 9. Since k=3 and nodes at level 3 are 7 and 9. + +// Time Complexity: O(n) where n is the number of nodes in the binary tree +// Space Complexity: O(n) where n is the number of nodes in the binary tree