
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Print leftmost and rightmost nodes of a Binary Tree in C Program.
Given a binary tree with left and right children and the task is to print the exact right and left child of the given tree.
Leftmost nodes are the nodes which are associated on the left side from the parent node of the tree and rightmost nodes are which are associated on the right side from the parent node of the root.
Example
Input: 106 20 320 100 21 61 52 Output: 106 20 320 100 52
Algorithm
Start Step 1 -> create structure of a node Declare int data Declare struct node *left and *right Step 2 -> create struct node* newNode(int val) Create node* temp=new node Set temp->data = val Set temp->left = temp->right = NULL return (temp) step 3 -> Declare Function void print(node *root) IF root == NULL Return Use STL queue<node*> que Call que.push(root) Use STL vector<int> ans Loop While !que.empty() Set int n = que.size() Loop for int i =0 and i<n and i++ Set node *temp = que.front() Set que.pop() IF i=0 Set ans.push_back(temp->data) End Else IF i=n-1 Set ans.push_back(temp->data) End If temp->left Set que.push(temp->left) End IF temp->right Set que.push(temp->right) End End Loop For auto i : ans Print i End Step 4 -> In main() Declare node *root = newNode(106) to create a node Call print(root) stop
Example
#include <bits/stdc++.h> using namespace std; //structure of a node { int data; struct node* left, *right; }; //structure to create a new node struct node* newNode(int val){ node* temp = new node; temp->data = val; temp->left = temp->right = NULL; return (temp); } //function to print corner elements of a tree void print(node *root) { if(root == NULL) return; queue<node*> que; que.push(root); vector<int> ans; while(!que.empty()){ int n = que.size(); for(int i =0;i<n;i++){ node *temp = que.front(); que.pop(); if(i==0) ans.push_back(temp->data); else if(i==n-1) ans.push_back(temp->data); if(temp->left) que.push(temp->left); if(temp->right) que.push(temp->right); } } for(auto i : ans) cout << i << " "; } int main (){ node *root = newNode(106); root->left = newNode(20); root->right = newNode(320); root->left->left = newNode(100); root->left->right = newNode(21); root->right->left = newNode(61); root->right->right = newNode(52); print(root); return 0; }
Output
if we run above program then it will generate following output
106 20 320 100 52
- Related Articles
- Print all internal nodes of a Binary tree in C++
- Print all full nodes in a Binary Tree in C++
- Program to print nodes in the Top View of Binary Tree using C++
- Print all odd nodes of Binary Search Tree in C++
- Print all even nodes of Binary Search Tree in C++
- Print Levels of all nodes in a Binary Tree in C++ Programming.
- Program to print nodes between two given level numbers of a binary tree using C++
- Print all nodes in a binary tree having K leaves in C++
- Program to print path from root to all nodes in a Complete Binary Tree using C++
- Print all leaf nodes of a binary tree from right to left in C++
- Print path between any two nodes in a Binary Tree in C++ Programming.
- Find distance between two nodes of a Binary Tree in C++ Program
- Print all nodes between two given levels in Binary Tree in C++
- Validate Binary Tree Nodes in C++
- Derive the string “abb” for leftmost and rightmost derivation

Advertisements