"Leetcode OJ" Same Tree

Source: Internet
Author: User

title : Given binary trees, write a function to check if they is equal or not.

The binary trees is considered equal if they is structurally identical and the nodes has the same value.

problem-Solving ideas : The basic meaning of this question is to determine whether two binary trees are the same, because the recursive thinking is not very skilled, so the beginning of a non-recursive method of writing, but the old one case can not pass. Later, with a recursive thinking, I finally passed all the test cases. Before each encounter recursive problems will be a little resistance, worry about a bit of bugs, usually practice very little, the following is a simple summary of the idea of recursion:

The program itself calls its own programming skills called recursion , there are four characteristics of recursion:

1. There must be a termination condition that can eventually be reached, otherwise the program will fall into an infinite cycle;

2. Sub-problems are smaller than the original problem on the scale, or closer to the termination conditions;

3. Sub-problem can be solved by recursive call again or by satisfying termination condition.

4. The solution of sub-problems should be combined into the solution of the whole problem.

The above problem is a typical recursive problem, first of all to determine whether two binary root node is the same, and then determine whether the child node is the same or not. The code is as follows:

/*determine if two binary trees are the same*/classTreeNode {intVal;    TreeNode left;    TreeNode right; TreeNode (intX) {val =x;} } Public classSolution { Public BooleanIssamenode (TreeNode A,treenode B)//determine if two nodes are the same    {        if(a==NULL&&b==NULL)//are empty nodes, the same            return true; Else if(a!=NULL&&b!=NULL&&a.val==b.val)//are not NULL nodes and the node values are equal, the same            return true; Else    //the rest of the situation is not the same            return false; }     Public BooleanIssametree (TreeNode p, TreeNode q) {if(p==NULL&&q==NULL)            return true; if(!Issamenode (P, q))return false; if(Issamenode (P, q))returnIssametree (P.left, Q.left) &&Issametree (P.right, q.right); return false; }}

"Leetcode OJ" Same 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.