Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions Tree_compare.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

template <typename T>
class TreeNode {
public:
T data;
vector<TreeNode<T>*> children;

TreeNode(T data) { this->data = data; }

~TreeNode() {
for (int i = 0; i < children.size(); i++) {
delete children[i];
}
}
};

bool areIdentical(TreeNode<int> *root1, TreeNode<int> * root2)
{

if(root1 == NULL && root2 == NULL)
{
return false;
}

if(root1->data == root2->data)
{
return true;
}

int x = root1->children.size();
int y = root2->children.size();

if(x ==y)
{
for(int i=0;i<x;i++)
{
if(areIdentical(root1->children[i],root2->children[i]))
{
return true;
}
return false;
}
}
else
{
return false;
}
}

TreeNode<int>* takeInputLevelWise() {
int rootData;
cin >> rootData;
TreeNode<int>* root = new TreeNode<int>(rootData);

queue<TreeNode<int>*> pendingNodes;

pendingNodes.push(root);
while (pendingNodes.size() != 0) {
TreeNode<int>* front = pendingNodes.front();
pendingNodes.pop();
int numChild;
cin >> numChild;
for (int i = 0; i < numChild; i++) {
int childData;
cin >> childData;
TreeNode<int>* child = new TreeNode<int>(childData);
front->children.push_back(child);
pendingNodes.push(child);
}
}

return root;
}

int main() {
TreeNode<int>* root1 = takeInputLevelWise();
TreeNode<int>* root2 = takeInputLevelWise();
cout << (areIdentical(root1, root2) ? "true" : "false");
}
75 changes: 75 additions & 0 deletions Tree_max_child_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

template <typename T>
class TreeNode {
public:
T data;
vector<TreeNode<T>*> children;

TreeNode(T data) { this->data = data; }

~TreeNode() {
for (int i = 0; i < children.size(); i++) {
delete children[i];
}
}
};

TreeNode<int>* maxSumNode(TreeNode<int>* root) {

if(root==NULL)
{
return NULL;
}
TreeNode<int> *result = root; // root node
int max = root->data; // root node
int childCount = root->children.size();
for(int i=0; i<childCount; i++)
{
TreeNode<int> *temp = maxSumNode(root->children[i]);
if(temp->data > max)
{
max = temp->data;
result = temp;
}
}
return result;
}

TreeNode<int>* takeInputLevelWise() {
int rootData;
cin >> rootData;
TreeNode<int>* root = new TreeNode<int>(rootData);

queue<TreeNode<int>*> pendingNodes;

pendingNodes.push(root);
while (pendingNodes.size() != 0) {
TreeNode<int>* front = pendingNodes.front();
pendingNodes.pop();
int numChild;
cin >> numChild;
for (int i = 0; i < numChild; i++) {
int childData;
cin >> childData;
TreeNode<int>* child = new TreeNode<int>(childData);
front->children.push_back(child);
pendingNodes.push(child);
}
}

return root;
}

int main() {
TreeNode<int>* root = takeInputLevelWise();

TreeNode<int>* ans = maxSumNode(root);

if (ans != NULL) {
cout << ans->data;
}
}