"Leetcode" symmetric Tree

Source: Internet
Author: User

Topic

Given a binary tree, check whether it is a mirror of the itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

    1   /   2   2/\/3  4 4  3

But the following are not:

    1   /   2   2   \      3    3
Answer

The topic asks whether the tree is mirrored symmetry.

Recursive method: To determine whether the left and right sub-trees are mirrored, to determine whether the left and right sub-tree node values are the same, and to ensure that the left dial hand nodes of the tree are the same as the right child nodes, Zuozi right child nodes and the left Dial hand node of the tree are the same, the code is as follows:

/** * Definition for Binary tree * public class TreeNode {*     int val, *     TreeNode left, *     TreeNode right; *
   
    treenode (int x) {val = x;} *} */public class Solution {public    Boolean issymmetric (TreeNode root) {        if (root==n ull) {            return true;        }        Return Ismirrorrec (root.left,root.right);    }        public static Boolean Ismirrorrec (TreeNode r1,treenode r2) {        if (r1==null&&r2==null) {            return true;        }        if (r1==null| | R2==null) {            return false;        }        If all two trees are non-empty, first compare the root node        if (r1.val!=r2.val) {            return false;        }        The image of the left subtree of the recursive comparison R1 is not the mirror of the R2 right subtree and the R1 right subtree is not the R2 left dial hand tree        return Ismirrorrec (r1.left,r2.right) &&ismirrorrec ( R1.right,r2.left);}    }
   


Iterative method: Save the nodes of the left and right subtree with two queues, compare the team header elements of the two queues each time (before you make sure two nodes are not empty), and if they are equal, continue to add their left and the right nodes to the corresponding queue (note the order of addition), loop join and take out until the queue is empty.

public Boolean issymmetric (TreeNode root) {if (Root==null) {return true;} Linkedlist<treenode> l=new linkedlist<treenode> (); This is used to simulate the queue linkedlist<treenode> r=new linkedlist<treenode> () l.add (root.left); R.add (root.right); while (!l.isempty () &&!r.isempty ()) {TreeNode tmp1=l.pop (); TreeNode Tmp2=r.pop (); if (tmp1==null&&tmp2!=null| | Tmp1!=null&&tmp2==null) {return false;} if (tmp1!=null) {if (Tmp1.val!=tmp2.val) {return false;} L.add (Tmp1.left); L.add (tmp1.right); R.add (tmp2.right); R.add (Tmp2.left);}} return true;}


---EOF---

"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.