In C language, print the right view of the binary tree
The task is to print the right node of the given binary tree. First the user will insert data to create a binary tree and then print a right view of the resulting tree.
The above image shows a binary tree created using nodes 10, 42, 93, 14, 35, 96, 57 and 88, with the ones selected and displayed on the right side of the tree node. For example, 10, 93, 57, and 88 are the rightmost nodes of the binary tree.
Example
Input : 10 42 93 14 35 96 57 88 Output : 10 93 57 88
Each node has two pointers, a left pointer and a right pointer. According to this question, the program only needs to traverse the right node. Therefore, the left child of the node does not need to be considered.
The right view stores all nodes that are the last node of their level. Therefore, we can simply use recursive methods to store and access nodes in such a way that the right subtree is traversed first and then the left subtree. Whenever the program detects that the level of a node is greater than the level of the previous node, the previous node is displayed because it will be the last node in its level.
The following code shows the C language implementation of a given algorithm
Algorithm
START Step 1 -> create node variable of type structure Declare int data Declare pointer of type node using *left, *right Step 2 -> create function for inserting node with parameter as item Declare temp variable of node using malloc Set temp->data = item Set temp->left = temp->right = NULL return temp step 3 -> Declare Function void right_view(struct node *root, int level, int *end_level) IF root = NULL Return IF *end_level < level Print root->data Set *end_level = level Call right_view(root->right, level+1, end_level) Call right_view(root->left, level+1, end_level) Step 4 -> Declare Function void right(struct node *root) Set int level = 0 Call right_view(root, 1, &level) Step 5 -> In Main() Pass the values for the tree nodes using struct node *root = New(10) Call right(root) STOP
The Chinese translation of
is:Example
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *left, *right; }; struct node *New(int item) { struct node *temp = (struct node *)malloc(sizeof(struct node)); temp->data = item; temp->left = temp->right = NULL; return temp; } void right_view(struct node *root, int level, int *end_level) { if (root == NULL) return; if (*end_level < level) { printf("%d\t", root->data); *end_level = level; } right_view(root->right, level+1, end_level); right_view(root->left, level+1, end_level); } void right(struct node *root) { int level = 0; right_view(root, 1, &level); } int main() { printf("right view of a binary tree is : "); struct node *root = New(10); root->left = New(42); root->right = New(93); root->left->left = New(14); root->left->right = New(35); root->right->left = New(96); root->right->right = New(57); root->right->left->right = New(88); right(root); return 0; }
Output
If we run the above program, it will generate the following output.
right view of a binary tree is : 10 93 57 88
The above is the detailed content of In C language, print the right view of the binary tree. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

C language functions are reusable code blocks. They receive input, perform operations, and return results, which modularly improves reusability and reduces complexity. The internal mechanism of the function includes parameter passing, function execution, and return values. The entire process involves optimization such as function inline. A good function is written following the principle of single responsibility, small number of parameters, naming specifications, and error handling. Pointers combined with functions can achieve more powerful functions, such as modifying external variable values. Function pointers pass functions as parameters or store addresses, and are used to implement dynamic calls to functions. Understanding function features and techniques is the key to writing efficient, maintainable, and easy to understand C programs.

The return value types of C language function include int, float, double, char, void and pointer types. int is used to return integers, float and double are used to return floats, and char returns characters. void means that the function does not return any value. The pointer type returns the memory address, be careful to avoid memory leakage.结构体或联合体可返回多个相关数据。

Methods to efficiently and elegantly find the greatest common divisor in C language: use phase division to solve by constantly dividing the remainder until the remainder is 0. Two implementation methods are provided: recursion and iteration are concise and clear, and the iterative implementation is higher and more stable. Pay attention to handling negative numbers and 0s, and consider performance optimization, but the phase division itself is efficient enough.

C language functions are reusable code blocks, receive parameters for processing, and return results. It is similar to the Swiss Army Knife, powerful and requires careful use. Functions include elements such as defining formats, parameters, return values, and function bodies. Advanced usage includes function pointers, recursive functions, and callback functions. Common errors are type mismatch and forgetting to declare prototypes. Debugging skills include printing variables and using a debugger. Performance optimization uses inline functions. Function design should follow the principle of single responsibility. Proficiency in C language functions can significantly improve programming efficiency and code quality.

The key elements of C function definition include: return type (defining the value returned by the function), function name (following the naming specification and determining the scope), parameter list (defining the parameter type, quantity and order accepted by the function) and function body (implementing the logic of the function). It is crucial to clarify the meaning and subtle relationship of these elements, and can help developers avoid "pits" and write more efficient and elegant code.

A function pointer is a pointer to a function, and a pointer function is a function that returns a pointer. Function pointers point to functions, used to select and execute different functions; pointer functions return pointers to variables, arrays or other functions; when using function pointers, pay attention to parameter matching and checking pointer null values; when using pointer functions, pay attention to memory management and free dynamically allocated memory; understand the differences and characteristics of the two to avoid confusion and errors.

The pointer parameters of C language function directly operate the memory area passed by the caller, including pointers to integers, strings, or structures. When using pointer parameters, you need to be careful to modify the memory pointed to by the pointer to avoid errors or memory problems. For double pointers to strings, modifying the pointer itself will lead to pointing to new strings, and memory management needs to be paid attention to. When handling pointer parameters to structures or arrays, you need to carefully check the pointer type and boundaries to avoid out-of-bounds access.

Algorithms are the set of instructions to solve problems, and their execution speed and memory usage vary. In programming, many algorithms are based on data search and sorting. This article will introduce several data retrieval and sorting algorithms. Linear search assumes that there is an array [20,500,10,5,100,1,50] and needs to find the number 50. The linear search algorithm checks each element in the array one by one until the target value is found or the complete array is traversed. The algorithm flowchart is as follows: The pseudo-code for linear search is as follows: Check each element: If the target value is found: Return true Return false C language implementation: #include#includeintmain(void){i
