Check If binary trees is identical. Identical means the binary trees has the same structure and every identical position has the same value.
Example
1 1 / \ / 2 2 and 2 2 / /4 4
is identical.
1 1 / \ / 2 3 and 2 3 / 4 4
is not identical.
1 /**2 * Definition of TreeNode:3 * public class TreeNode {4 * public int val;5 * Public TreeNode left and right;6 * Public TreeNode (int val) {7 * This.val = val;8 * This.left = This.right = null;9 * }Ten * } One */ A Public classSolution { - /** - * @param A, B, the root of binary trees. the * @return True if they is identical, or false. - */ - PublicBoolean isidentical (TreeNode A, TreeNode b) { - if(A = =NULL&& b = =NULL)return true; + if(A = =NULL|| b = =NULL)return false; - + if(A.val! = b.val)return false; A at returnIsidentical (A.left, B.left) &&isidentical (A.right, b.right); - } -}
Identical Binary Tree