Leetcode-symmetric Tree

Source: Internet
Author: User

The question is whether a binary tree is symmetrical, such as the first tree.

At first my idea was very complex, first to serialize the tree into an array, and then to judge by the subscript to calculate the symmetrical position.

Later, it was found that the second layer two nodes above are p and q to determine if P's left child is equal to Q's right child, and P's right child is equal to Q's left child.

/** thought is the hierarchy traversal, each time two nodes out of the team, for P and Q, if meet P.left=q.right and P.right=q.left, then queued until the queue is empty. * @author Admin * */public class Symmetrictree {public static Boolean issymmetric (TreeNode root) {treenode[] queue=new Tre enode[256];//This best uses ArrayList instead of int head=0;int tail=0;if (root==null) {//If the root is empty return true;} else if (root.leftchild==null &&root.rightchild==null) {//If the left child and right child are both empty return true;} else if (root.leftchild!=null&& Root.rightchild!=null&&root.leftchild.val==root.rightchild.val) {//If both are not empty and the values are equal queue[tail++]= root.leftchild;//left child team queue[tail++]=root.rightchild;//right child queue} else {//If both of them are empty return false;} Boolean Flag=true;while (Head!=tail&&flag) {//When the queue is not empty and Flag==true TreeNode node1=queue[head++];//out of the team TreeNode node2=queue[head++];//determines whether the Node1 left child and Node2 's right child are equal if (node1.leftchild!=null| | Node2.rightchild!=null) {if (node1.leftchild!=null&&node2.rightchild!=null) {if (node1.leftChild.val== Node2.rightChild.val) {Queue[tail++]=node1.leftchild;queue[tail++]=node2.rightchild;} else {return false;}} else {return false;}} Determine if the right child of the Node1 and Node2 's left child are equal if (node1.rightchild!=null| | Node2.leftchild!=null) {if (node1.rightchild!=null&&node2.leftchild!=null) {if (node1.rightchild.val== Node2.leftChild.val) {Queue[tail++]=node1.rightchild;queue[tail++]=node2.leftchild;} else {flag=false;}} else {return false;}}}    return flag; }}

But after seeing the discussion, we can use recursion or stack to realize it.

Use of the pointers (p, Q), traverse the tree simultaneously, p goes in-order while Q goes reversed In-order, compare along t He. It can be do with pre-order and reversed pre-order or post-order and reversed post-order as well.

public Boolean issymmetric (TreeNode root) {    return traverse (root, root);} Private Boolean traverse (TreeNode p, TreeNode q) {    if (p = = NULL && q = = null) return true;    if (p = = NULL | | q = NULL) return false;    if (!traverse (P.left, q.right))         return false;    if (p.val! = Q.val) return false;    return Traverse (P.right, q.left);}

So I came up with the following solution.

public static Boolean issymmetrictree (TreeNode root) {return traverse (root,root);} public static Boolean traverse (TreeNode p,treenode q) {//If both are empty, then Trueif (p==null&&q==null) {return true;} Falseif if one of the two is not empty (p==null| | Q==null) {return false;} Determine if the value is equal to return P.val==q.val&&traverse (p.left,q.right) &&traverse (p.right,q.left);}

In fact, many of the tree problems can be used to solve the problem, the first to find a certain rule, and then return to the solution is very easy.

For example, determine whether two trees are equal.

public static Boolean Issametree (TreeNode p, TreeNode q) {        return traverse (P, q);    } public static Boolean traverse (TreeNode p,treenode Q) {if (p==null&&q==null) {return true;} if (p==null| | Q==null) {return false;} Return P.val==q.val&&traverse (p.right,q.right) &&traverse (p.left,q.left);}

  

Leetcode-symmetric 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.