Leetcode same Tree

Source: Internet
Author: User

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.

Test instructions: Judge whether the two trees are the same.

Ideas: One is recursion, one is to use the queue to do hierarchical traversal judgment.

/** * Definition for Binary tree * public class TreeNode {*     int val, *     TreeNode left, *     TreeNode right; *
   
    treenode (int x) {val = x;} *} */public class Solution {public    boolean issametree (TreeNode p, TreeNode q) {    if (p = = NULL && q = = null) return true;    else if (P! = NULL && q = = null) return false;    else if (p = = NULL && Q! = null) return false;    else {    if (p.val! = Q.val)     return false;    else return Issametree (P.left, Q.left) && issametree (P.right, q.right);}}}    
   

/** * Definition for Binary tree * public class TreeNode {* int val, * TreeNode left, * TreeNode right; TreeNode (int x) {val = x;} *} */public class Solution {Private Boolean Issamenode (TreeNode p, TreeNode q) {if (P = = NULL && q = = null) return True;if ((P! = NULL && q = = null) | | (p = = NULL && Q! = null) | | (P.val! = q.val))    return False;return true;}    public boolean issametree (TreeNode p, TreeNode q) {if (!issamenode (p, Q)) return false;        if (p = = NULL && q = = null) return true;    Linkedlist<treenode> left = new linkedlist<treenode> ();    linkedlist<treenode> right = new linkedlist<treenode> ();    Left.add (P);    Right.add (q);    while (Left.size ()! = 0 && right.size ()! = 0) {TreeNode ltmp = Left.peekfirst ();    TreeNode rtmp = Right.peekfirst ();    Left.removefirst ();        Right.removefirst ();    if (!issamenode (Ltmp.left, Rtmp.left)) return false; if (ltmp.left! = null &Amp;& Rtmp.left! = null) {Left.add (ltmp.left);    Right.add (Rtmp.left);    } if (!issamenode (Ltmp.right, Rtmp.right)) return false;    if (ltmp.right! = NULL && rtmp.right! = null) {Left.add (ltmp.right);    Right.add (Rtmp.right);    }} return true; }}



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