Example: 1 1 / \ / \ 2 3 != 2 3 / \ / \ / \ / \ 4 5 6 7 4 5 6 7 / 8
1. The base condition of recursion is that if both the roots
are null
, return true
.
2. If only one of them is null
, return false
.
3. Compare the data of the two nodes. Return (root1.data == root2.data && isIdentical(root1.left, root2.left) && isIdentical(root1.right, root2.right))
public boolean isIdentical(TreeNode root1, TreeNode root2) { }
C
Java
Python