Professional Documents
Culture Documents
Tree
Tree
Tree
- Sum
Sub-Topic (Example: name of college)
Key idea
• Initialize the first element as root
1. root
2. root -> left
3. Root -> right
Sum 9
8
6
16
13
1 4
2
BSTTopic/Course
- Diameter
Sub-Topic (Example: name of college)
Key idea
• Initialize the first element as root
Input: 1 2 3 4 5
2
5
BSTTopic/Course
Sub-Topic (Example: name of college)
1. Create a recursive function that takes a node and the two values n1 & n2
2. If (current node <= n1 & n2), LCA lies in the right subtree
3. Call the recursive function -> right subtree
4. If (current node >= n1 and n2), LCA lies in the left subtree
5. Call the recursive function -> left subtree
6. If both the above cases are false, then return the current node as LCA
Key idea
Input:Ancestor
Common 6 3 1 4 2(3, 6) 6
Output: 6
3
1 4
2
BSTTopic/Course
- Kth Smallest Element
Sub-Topic (Example: name of college)
Key idea
• Initialize the first element as root
• Next check the element -> Insertion up to ‘-1’
70
60 20 40 60 80
80