Проверьте, приводит ли обратный порядок уровней двоичного дерева к палиндрому
Деревья, Структуры данных
Дано бинарное дерево и задание, если проверить, приводит ли его обход уровня порядка к палиндрому или нет.
Примеры:
Input:
Output: Yes
RADAR is the level order traversal of the
given tree which is a palindrome.
Input:
Output: No
Подходить:
- Пройдите двоичное дерево в порядке уровней и сохраните узлы в стеке.
- Обходите двоичное дерево в порядке уровней еще раз и сравните данные в узле с данными в верхней части стека.
- В случае совпадения перейдите к следующему узлу.
- В случае несоответствия остановите и напечатайте №
Ниже приведена реализация вышеуказанного подхода:
C ++
#include <bits/stdc++.h>
using namespace std;
struct node {
char data;
node *left, *right;
};
node* add( char data)
{
node* newnode = new node;
newnode->data = data;
newnode->left = newnode->right = NULL;
return newnode;
}
void findInv(node* root, stack<node*>& S)
{
if (root == NULL)
return ;
queue<node*> Q;
Q.push(root);
while (Q.size()) {
node* temp = Q.front();
Q.pop();
S.push(temp);
if (temp->left != NULL)
Q.push(temp->left);
if (temp->right != NULL)
Q.push(temp->right);
}
}
bool isPalindrome(stack<node*> S, node* root)
{
queue<node*> Q;
Q.push(root);
while (Q.size()) {
node* temp = Q.front();
node* temp1 = S.top();
S.pop();
Q.pop();
if (temp->data != temp1->data)
return false ;
if (temp->left != NULL)
Q.push(temp->left);
if (temp->right != NULL)
Q.push(temp->right);
}
return true ;
}
int main()
{
node* root = add( 'R' );
root->left = add( 'A' );
root->right = add( 'D' );
root->left->left = add( 'A' );
root->left->right = add( 'R' );
stack<node*> S;
findInv(root, S);
if (isPalindrome(S, root))
cout << "Yes" ;
else
cout << "NO" ;
return 0;
}
|
Джава
import java.util.*;
class GFG
{
static class node
{
char data;
node left, right;
};
static node add( char data)
{
node newnode = new node();
newnode.data = data;
newnode.left = newnode.right = null ;
return newnode;
}
static void findInv(node root, Stack<node> S)
{
if (root == null )
return ;
Queue<node> Q = new LinkedList<>();
Q.add(root);
while (Q.size() > 0 )
{
node temp = Q.peek();
Q.remove();
S.add(temp);
if (temp.left != null )
Q.add(temp.left);
if (temp.right != null )
Q.add(temp.right);
}
}
static boolean isPalindrome(Stack<node> S, node root)
{
Queue<node> Q = new LinkedList<>();
Q.add(root);
while (Q.size() > 0 )
{
node temp = Q.peek();
node temp1 = S.peek();
S.pop();
Q.remove();
if (temp.data != temp1.data)
return false ;
if (temp.left != null )
Q.add(temp.left);
if (temp.right != null )
Q.add(temp.right);
}
return true ;
}
public static void main(String[] args)
{
node root = add( 'R' );
root.left = add( 'A' );
root.right = add( 'D' );
root.left.left = add( 'A' );
root.left.right = add( 'R' );
Stack<node> S = new Stack<node>();
findInv(root, S);
if (isPalindrome(S, root))
System.out.print( "Yes" );
else
System.out.print( "NO" );
} }
|
C #
using System;
using System.Collections.Generic;
class GFG
{
class node
{
public char data;
public node left, right;
};
static node add( char data)
{
node newnode = new node();
newnode.data = data;
newnode.left = newnode.right = null ;
return newnode;
}
static void findInv(node root, Stack<node> S)
{
if (root == null )
return ;
Queue<node> Q = new Queue<node>();
Q.Enqueue(root);
while (Q.Count > 0)
{
node temp = Q.Peek();
Q.Dequeue();
S.Push(temp);
if (temp.left != null )
Q.Enqueue(temp.left);
if (temp.right != null )
Q.Enqueue(temp.right);
}
}
static bool isPalindrome(Stack<node> S,
node root)
{
Queue<node> Q = new Queue<node>();
Q.Enqueue(root);
while (Q.Count > 0)
{
node temp = Q.Peek();
node temp1 = S.Peek();
S.Pop();
Q.Dequeue();
if (temp.data != temp1.data)
return false ;
if (temp.left != null )
Q.Enqueue(temp.left);
if (temp.right != null )
Q.Enqueue(temp.right);
}
return true ;
}
public static void Main(String[] args)
{
node root = add( 'R' );
root.left = add( 'A' );
root.right = add( 'D' );
root.left.left = add( 'A' );
root.left.right = add( 'R' );
Stack<node> S = new Stack<node>();
findInv(root, S);
if (isPalindrome(S, root))
Console.Write( "Yes" );
else
Console.Write( "NO" );
} }
|
Выход:
Yes
Рекомендуемые посты:
Проверьте, приводит ли обратный порядок уровней двоичного дерева к палиндрому
0.00 (0%) 0 votes