Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a9de362

Browse files
updated Notes
1 parent 092aa1d commit a9de362

10 files changed

+145
-3
lines changed

‎.vscode/tasks.json‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"tasks": [
3+
{
4+
"type": "cppbuild",
5+
"label": "C/C++: g++.exe build active file",
6+
"command": "C:\\MinGW\\bin\\g++.exe",
7+
"args": [
8+
"-fdiagnostics-color=always",
9+
"-g",
10+
"${file}",
11+
"-o",
12+
"${fileDirname}\\${fileBasenameNoExtension}.exe"
13+
],
14+
"options": {
15+
"cwd": "${fileDirname}"
16+
},
17+
"problemMatcher": [
18+
"$gcc"
19+
],
20+
"group": {
21+
"kind": "build",
22+
"isDefault": true
23+
},
24+
"detail": "Task generated by Debugger."
25+
}
26+
],
27+
"version": "2.0.0"
28+
}

‎20BoundryTraversal.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
using namespace std;
1818

19+
20+
1921
// Node structure for the binary tree
2022
struct Node {
2123
int data;

‎26(1)rootToLeafAllPaths.exe‎

86.8 KB
Binary file not shown.

‎26(1)rootToLeafAllPaths.cpp‎ renamed to ‎26ArootToLeafAllPaths.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int main(){
5050
root->left->left->left = new TreeNode(3);
5151
root->left->left->right = new TreeNode(2);
5252
root->left->right = new TreeNode(5);
53-
root->right = new TreeNode(7);
53+
root->right = new TreeNode(3);
5454
root->right->left = new TreeNode(9);
5555
root->right->left->right = new TreeNode(2);
5656
root->right->left->right->right = new TreeNode(4);

‎26ArootToLeafAllPaths.exe‎

102 KB
Binary file not shown.

‎27lowestCommonAncestors.cpp‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
struct TreeNode
4+
{
5+
int data;
6+
struct TreeNode* left;
7+
struct TreeNode* right;
8+
9+
TreeNode(int val)
10+
{
11+
data = val;
12+
left = NULL;
13+
right = NULL;
14+
}
15+
16+
};
17+
// Question In this part we have to find the lowest ancestor of both lowest ancestors means that the node whch connects both
18+
19+
// Initution Our Intitution to solve the Question is to store the root to node path of both the nodes in a vector and then compare the paths of both the nodes the last node where they become unequal is the the answer
20+
bool helper(TreeNode* root,TreeNode* node,vector<TreeNode*> & arr){
21+
if(root==NULL){
22+
return false;
23+
}
24+
25+
arr.push_back(root);
26+
27+
if(root==node) return true;
28+
29+
if((helper(root->left,node,arr))||(helper(root->right,node,arr)))
30+
return true;
31+
32+
arr.pop_back();
33+
}
34+
TreeNode* LCAof2Nodes(TreeNode* root,TreeNode* A,TreeNode* B){
35+
vector<TreeNode*> firstTrack;
36+
vector<TreeNode*> secondTrack;
37+
38+
helper(root,A,firstTrack);
39+
helper(root,B,secondTrack);
40+
41+
int m = firstTrack.size();
42+
int n = secondTrack.size();
43+
44+
TreeNode* ans = firstTrack[0];
45+
46+
for(int i=1;i<min(m,n);i++){
47+
if(firstTrack[i]==secondTrack[i]){
48+
ans = firstTrack[i];
49+
}
50+
else {
51+
return ans;
52+
}
53+
}
54+
return ans;
55+
56+
}
57+
58+
// the previous approach is using a extra space which can be reduced by optimizing the solution the problem approach
59+
//we will do the DFS in the tree and each node in the tree will return the NULL untill we get the given node for exampled if any node has the same value as one the two given nodes then it will return only its node the TreeNode which gets both the node will be our answer
60+
61+
TreeNode* LCAof2Nodes2(TreeNode* root,TreeNode* A,TreeNode* B){
62+
if(root == NULL || root==A || root==B ){
63+
return root;
64+
}
65+
66+
TreeNode* left = LCAof2Nodes2(root->left,A,B);
67+
TreeNode* right = LCAof2Nodes2(root->right,A,B);
68+
69+
if(left==NULL){
70+
return left;
71+
}
72+
else if(right==NULL){
73+
return right;
74+
}
75+
else return root;
76+
}
77+
int main(){
78+
struct TreeNode* root = new TreeNode(2);
79+
root->left = new TreeNode(5);
80+
root->left->left = new TreeNode(4);
81+
root->left->left->left = new TreeNode(3);
82+
root->left->left->right = new TreeNode(2);
83+
root->left->right = new TreeNode(5);
84+
root->right = new TreeNode(3);
85+
root->right->left = new TreeNode(9);
86+
root->right->left->right = new TreeNode(2);
87+
root->right->left->right->right = new TreeNode(4);
88+
root->right->left->right->left = new TreeNode(1);
89+
root->right->left->left = new TreeNode(3);
90+
root->right->right = new TreeNode(7);
91+
92+
93+
cout<<LCAof2Nodes(root,root->left->left->right, root->right->right)->data<<endl;
94+
95+
96+
97+
}

‎27lowestCommonAncestors.exe‎

68.3 KB
Binary file not shown.

‎README.md‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,19 @@
22
# Teacher - Raj Vikramaditya
33
# Start_Date - 02/07/2024
44
# Source - YouTube
5-
# LINK - https://www.youtube.com/watch?v=Bfqd8BsPVuw&list=PLkjdNRgDmcc0Pom5erUBU4ZayeU9AyRRu&index=9
5+
# LINK - https://www.youtube.com/watch?v=Bfqd8BsPVuw&list=PLkjdNRgDmcc0Pom5erUBU4ZayeU9AyRRu&index
6+
## Folder - Tree Notes
7+
8+
This folder contains notes and resources related to the topic of trees in computer science. The content in this folder is based on the teachings of Raj Vikramaditya, a renowned teacher in the field.
9+
10+
### Start Date
11+
The learning journey for this topic started on 02/07/2024.
12+
13+
### Source
14+
The primary source of information for this topic is a YouTube playlist created by Raj Vikramaditya. The playlist covers various aspects of trees and provides valuable insights and explanations.
15+
16+
### YouTube Playlist Link
17+
You can access the YouTube playlist by clicking [here](https://www.youtube.com/watch?v=Bfqd8BsPVuw&list=PLkjdNRgDmcc0Pom5erUBU4ZayeU9AyRRu&index).
18+
19+
Feel free to explore the content in this folder and enhance your understanding of trees in computer science.
20+

‎practice.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ vector<int> iterativeInOrderTraversalUsingStack(TreeNode* root){
5959

6060

6161

62-
62+
#write a function
6363
int main(){
6464

6565
struct TreeNode* root = new TreeNode(2);

‎practice.exe‎

-141 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /