Skip to content

Commit abbd4d2

Browse files
committed
added code for merge trees
1 parent f603e21 commit abbd4d2

File tree

2 files changed

+105
-3
lines changed

2 files changed

+105
-3
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,7 @@ Include contains single header implementation of data structures and some algori
226226
| Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example : for k = 3, n = 9 result would be [[1,2,6], [1,3,5], [2,3,4]], similarly for k = 3, n = 7, result would be [[1,2,4]]. | [combinationSum3.cpp](leet_code_problems/combinationSum3.cpp) |
227227
| Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. Follow up: Could you do it without any loop/recursion in O(1) runtime?| [addDigits.cpp](leet_code_problems/addDigits.cpp) |
228228
| Given a matrix with cell values 0 or 1. Find the length of the shortest path from (a1, b1) to (a2, b2), such that path can only be constructed through cells which have value 1 and you can only travel in 4 possible directions, i.e. left, right, up and down.|[shortest_path_maze.cpp](leet_code_problems/shortest_path_maze.cpp) |
229-
| The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
230-
Given two integers x and y, calculate the Hamming distance.| [hamming_distance.cpp](leet_code_problems/hamming_distance.cpp)|
231-
229+
| The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance.| [hamming_distance.cpp](leet_code_problems/hamming_distance.cpp)|
230+
| Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.| [merge_trees.cpp](leet_code_problems/merge_trees.cpp)|
232231

233232

leet_code_problems/merge_trees.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Given two binary trees and imagine that when you put one of them to cover the other, some nodes
3+
of the two trees are overlapped while the others are not.
4+
5+
You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then
6+
sum node values up as the new value of the merged node.
7+
Otherwise, the NOT null node will be used as the node of new tree.
8+
9+
Input:
10+
Tree 1 Tree 2
11+
1 2
12+
/ \ / \
13+
3 2 1 3
14+
/ \ \
15+
5 4 7
16+
Output:
17+
Merged tree:
18+
3
19+
/ \
20+
4 5
21+
/ \ \
22+
5 4 7
23+
24+
*/
25+
26+
27+
#include <iostream>
28+
29+
struct Node {
30+
int value;
31+
Node* left;
32+
Node* right;
33+
Node(int val):
34+
value{val}, left{nullptr}, right{nullptr}{}
35+
};
36+
37+
Node* mergeTrees(Node* t1, Node* t2)
38+
{
39+
if (!t1)
40+
{
41+
return t2;
42+
}
43+
44+
if (!t2)
45+
{
46+
return t1;
47+
}
48+
t1->value += t2->value;
49+
t1->left = mergeTrees(t1->left, t2->left);
50+
t1->right = mergeTrees(t1->right, t2->right);
51+
return t1;
52+
}
53+
54+
void preOrder(Node* t1)
55+
{
56+
if (t1)
57+
{
58+
std::cout << t1->value << " ";
59+
if (t1->left)
60+
{
61+
preOrder(t1->left);
62+
}
63+
else
64+
{
65+
std::cout << "null" << " ";
66+
}
67+
if (t1->right)
68+
{
69+
preOrder(t1->right);
70+
}
71+
else
72+
{
73+
std::cout << "null" << " ";
74+
}
75+
}
76+
}
77+
78+
int main()
79+
{
80+
Node* t1 = new Node(1);
81+
t1->left = new Node(3);
82+
t1->right = new Node(2);
83+
t1->left->left = new Node(5);
84+
std::cout << "Tree 1:";
85+
preOrder(t1);
86+
std::cout << std::endl;
87+
88+
Node* t2 = new Node(2);
89+
t2->left = new Node(1);
90+
t2->right = new Node(3);
91+
t2->left->right = new Node(4);
92+
t2->right->right = new Node(7);
93+
std::cout << "Tree 2:";
94+
preOrder(t2);
95+
std::cout << std::endl;
96+
97+
Node* t3 = mergeTrees(t1, t2);
98+
std::cout << "Tree 3:";
99+
preOrder(t3);
100+
std::cout << std::endl;
101+
102+
return 0;
103+
}

0 commit comments

Comments
 (0)