难度: 简单 标题:相同二叉树
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
判断两颗二叉树是否相同。
思路
递归,当左子树相同,右子树相同,值相同时两棵树相同。
取子树之前判断一下是否为 null。否则null.left,null.right,null.val会报错。
代码
1 | /** |