Skip to content

Commit d40d7b0

Browse files
Add 16th July problem optimized code
1 parent 7b8c346 commit d40d7b0

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Intuition
2+
We are given the task to find the path from a start node to a destination node. We can confidently say that the highest point of the path from the start node to the destination node is the Lowest Common Ancestor (LCA) of both the start and destination nodes. So, first, we will find the LCA node. Then, we will find the path from the LCA to the start node and from the LCA to the destination node. For the start node, we will count only the direction as 'U'because, in reality, we are coming from the start node to the LCA and then from the LCA to the destination node. Therefore, every step from the startValue node to the LCA node contributes only 'U', and the remaining path from the LCA to the destValue node contributes 'L' and 'R'.
3+
4+
Approach
5+
Find the Lowest Common Ancestor (LCA):
6+
7+
The LCA of two nodes in a binary tree is the deepest node that is an ancestor of both nodes.
8+
The intuition here is that the path from the startValue node to the destValue node can be broken down into two segments:
9+
From the startValue node to the LCA.
10+
From the LCA to the destValue node.
11+
12+
Construct Paths:
13+
Once the LCA is identified, the next step is to find the path from the LCA to the startValue node, and the path from the LCA to the destValue node.
14+
For the path to the startValue node, since we need to move upwards (denoted by 'U'), we traverse the tree from the LCA to the startValue and note the path.
15+
For the path to the destValue node, we traverse the tree from the LCA to the destValue and note the path using 'L' for left and 'R' for right.
16+
17+
Concatenate Paths:
18+
The final path from startValue to destValue is constructed by concatenating the upward path from startValue to the LCA and the downward path from the LCA to destValue.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution
13+
{
14+
public:
15+
TreeNode *LCA(TreeNode *root, int &s, int &e)
16+
{
17+
if (root == NULL)
18+
return NULL;
19+
if (root->val == s || root->val == e)
20+
return root;
21+
TreeNode *left = LCA(root->left, s, e);
22+
TreeNode *right = LCA(root->right, s, e);
23+
if (!left)
24+
return right;
25+
if (!right)
26+
return left;
27+
return root;
28+
}
29+
void LCATOStart(TreeNode *root, int val, string &curr, string &startpath)
30+
{
31+
if (!root)
32+
return;
33+
if (root->val == val)
34+
{
35+
startpath = curr;
36+
return;
37+
}
38+
curr.push_back('U');
39+
LCATOStart(root->left, val, curr, startpath);
40+
LCATOStart(root->right, val, curr, startpath);
41+
curr.pop_back();
42+
}
43+
void LCATODest(TreeNode *root, int val, string &curr, string &destPath)
44+
{
45+
if (!root)
46+
return;
47+
if (root->val == val)
48+
{
49+
destPath = curr;
50+
return;
51+
}
52+
curr.push_back('L');
53+
LCATODest(root->left, val, curr, destPath);
54+
curr.pop_back();
55+
curr.push_back('R');
56+
LCATODest(root->right, val, curr, destPath);
57+
curr.pop_back();
58+
}
59+
string getDirections(TreeNode *root, int startValue, int destValue)
60+
{
61+
TreeNode *lca = LCA(root, startValue, destValue);
62+
string startPath;
63+
string destPath;
64+
string curr;
65+
LCATOStart(lca, startValue, curr, startPath);
66+
LCATODest(lca, destValue, curr, destPath);
67+
return startPath + destPath;
68+
}
69+
};

0 commit comments

Comments
 (0)