Identical Binary Tree

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.