Code Logo

Tree Same Tree

Published at25 Jul 2026
Binary Tree Easy 0 views
Like0

Given two arrays representing binary trees, determine if they are identical (same structure and same values at each position).

Example Input & Output

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

Different sizes

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

Single node

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

Different values

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

Identical trees

Example 5
Input
[],[1]
Output
false
Explanation

Different sizes

Algorithm Flow

Recommendation Algorithm Flow for Tree Same Tree
Recommendation Algorithm Flow for Tree Same Tree

Solution Approach

function solution(a, b) {
  if (a.length !== b.length) return false;
  for (var i = 0; i < a.length; i++) { if (a[i] !== b[i]) return false; }
  return true;
}

Best Answers

java
class Solution {
    public boolean solution(int[] a, int[] b) {
        return java.util.Arrays.equals(a,b);
    }
}