Data Structure & Algorithm: Level-1 Problem #19. Identical Binary Trees

Two binary trees are identical when they have same data and arrangement of data is also same.

To identify if two trees are identical, we need to traverse both trees simultaneously, and while traversing we need to compare data and children of the trees.

Sample Input:

t1 = [1,2,3], t2 = [1,2,3]

Sample Output:

True

Compare Tree

With the sample input, t1 and t2 are identical binary trees.


数据结构和算法:初级练习题 #19 – 相同二叉树


如果两颗二叉树的数据相同并且数据的排列也相同,那么它们是相同的二叉树。

为了确定两棵二叉树树是否相同,我们需要同时遍历这两棵树,在遍历时比较每一个节点的数据以及它的子二叉树。

给定两颗二叉树,确认它们是否相同。

输入样例:

t1 = [1,2,3], t2 = [1,2,3]

输出样例:

True

上面的输入样例中,二叉树t1和t2是相同的。


Python Solution

Identical Trees