Code Logo

Invert Binary Tree

Published at25 Jul 2026
Easy 0 views
Like0

Invert (reverse) an array. This simulates inverting a binary tree where left and right children of all nodes are swapped.

Example Input & Output

Example 1
Input
[1,2,3,4]
Output
[4,3,2,1]
Explanation

Four elements

Example 2
Input
[]
Output
[]
Explanation

Empty

Example 3
Input
[1,2]
Output
[2,1]
Explanation

Simple inversion

Example 4
Input
[1]
Output
[1]
Explanation

Single element

Example 5
Input
[4,2,7,1,3,6,9]
Output
[9,6,3,1,7,2,4]
Explanation

Invert/reverse the array simulates tree mirror

Algorithm Flow

Recommendation Algorithm Flow for Invert Binary Tree
Recommendation Algorithm Flow for Invert Binary Tree

Solution Approach

function solution(arr) {
  return arr.slice().reverse();
}

Best Answers

java
class Solution {
    public int[] solution(int[] nums) {
        int[] r=nums.clone();
        for(int i=0,j=r.length-1;i<j;i++,j--){int t=r[i];r[i]=r[j];r[j]=t;}
        return r;
    }
}