Tree Find Parent Index
Published at25 Jul 2026
Created by M Ichsanul Fadhil
Given an array representing a binary tree (root at index 0, children at 2i+1 and 2i+2), find the parent index of a given node index. The root (index 0) has no parent, return -1.
For any node at index i, its parent is at index Math.floor((i-1)/2).
Example Input & Output
Example 1
Input
[1,2,3,4,5],3
Output
1
Explanation
Parent of index 3 is index 1
Example 2
Input
[1,2,3,4,5],1
Output
0
Explanation
Parent of index 1 is index 0
Example 3
Input
[1,2,3],2
Output
0
Explanation
Parent of index 2 is index 0
Example 4
Input
[1,2,3,4,5],0
Output
-1
Explanation
Root has no parent
Example 5
Input
[1,2,3,4,5],4
Output
1
Explanation
Parent of index 4 is index 1
Algorithm Flow

Recommendation Algorithm Flow for Tree Find Parent Index
Solution Approach
Best Answers
java
class Solution {
public int solution(int[] nums, int i) {
return i<=0||i>=nums.length?-1:(i-1)/2;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
