-
Notifications
You must be signed in to change notification settings - Fork 1
/
Count-Palindromies-inA-Tree.cpp
141 lines (128 loc) · 3.13 KB
/
Count-Palindromies-inA-Tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// A Binary Tree is constructed which represents various number palindromes on its paths starting from root to leaf. For example, the following tree represents palindromes like 121, 1331:
// 1
// / \
// 2 3
// / / \
// 1 6 3
// \ /
// 2 1
// The task is to count total number of palindromic paths in any given binary tree.
// Input is in the form of an array, where nodes are entered in level order, where empty nodes are represented by -1. The first line of input represents total nodes including the empty nodes in the complete binary tree. Output is the total count of nodes.
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *left, *right;
};
Node *newNode(int n)
{
Node *ptr=new Node;
ptr->data=n;
ptr->left=ptr->right=NULL;
return ptr;
}
Node * createBalancedBST(int arr[], int start, int end)
{
if(start > end)
return NULL;
int mid = (start+end)/2;
Node *root = newNode(arr[mid]);
root->left = createBalancedBST(arr, start, mid-1);
root->right = createBalancedBST(arr, mid+1, end);
return root;
}
bool hasPath(Node *root, vector<int>& arr, int x)
{
if (!root)
return false;
arr.push_back(root->data);
if (root->data == x)
return true;
if (hasPath(root->left, arr, x) ||
hasPath(root->right, arr, x))
return true;
arr.pop_back();
return false;
}
int findHeight(struct Node* node)
{
if (node == NULL)
return 0;
int leftHeight = findHeight(node->left);
int rightHeight = findHeight(node->right);
return 1 + (leftHeight > rightHeight ?leftHeight : rightHeight);
}
bool isPalindrome(string str)
{
int l = 0;
int h = str.length() - 1;
while (h > l)
{
if (str[l++] != str[h--])
{
return false;
}
}
return true;
}
bool isPathPal(int* path, int index)
{
int i = 0;
string s;
while (i <= index) {
s += to_string(path[i]);
i += 1;
}
return isPalindrome(s);
}
void printPalPath(int* path, int index)
{
for (int i = 0; i < index; i++) ;
cout<<index/2;
exit(0);
}
void printPath(struct Node* node,int* path, int index)
{
if (node == NULL)
{
return;
}
path[index] = node->data;
printPath(node->left, path,index + 1);
printPath(node->right, path,index + 1);
if (node->left == NULL &&node->right == NULL)
{
if (isPathPal(path, index))
{
printPalPath(path, index + 1);
}
}
else
{
cout<<"1";
exit(0);
}
}
void PalindromicPath(struct Node* node)
{
int height = findHeight(node);
int* path = new int[height];
memset(path, 0, sizeof(path));
printPath(node, path, 0);
}
int main()
{
int size,n;
cin>>size;
int a[size];
Node* root;
for(int i=0;i<size;i++)
{
cin>>a[i];
}
sort((a), a + size);
root = createBalancedBST(a, 0, size-1);
PalindromicPath(root);
return 0;
}