Leetcode same Tree

Source: Internet
Author: User

1. Topics


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.


2. Solution 1


Class Solution {Public:bool Issametree (TreeNode *p, TreeNode *q) {if (!p &&!q) return true;        else if (!p && q) return false;        else if (P &&!q) return false;            else {if (p->val! = Q->val) return false;                else {queue<treenode*> LQ;                Queue<treenode*> RQ;                Lq.push (P);                Rq.push (q);                    while (!lq.empty () &&!rq.empty ()) {treenode* Lfront = Lq.front ();                    treenode* Rfront = Rq.front ();                    Lq.pop ();                    Rq.pop (); if (!lfront->left &&!rfront->left);//null else if (!lfront->le                    FT && Rfront->left) return false; else if (Lfront->left &&!rfront-&Gt;left) return false;                            else {if (lfront->left->val! = rfront->left->val)                        return false;                            else {Lq.push (lfront->left);                        Rq.push (Rfront->left); }} if (!lfront->right &&!rfront->right);//Nu                    ll else if (!lfront->right && rfront->right) return false;                    else if (lfront->right &&!rfront->right) return false;                            else {if (lfront->right->val! = rfront->right->val)                        return false; else {Lq.push (lfront->right);                        Rq.push (Rfront->right);            }}} return true; }        }    }};

ideas: Very simple, breadth traversal can be judged more cumbersome, to the left and right node values are the same.


3. Solution 2


Class Solution {public:    bool Issametree (TreeNode *p, TreeNode *q) {        if (p = = NULL && q = = null) return true; else if (p = = NULL | | q = NULL) return False;bool islefttreesame = Issametree (P->left, q->left); bool Isrighttreesame = Issametree (p->right,q->right); return p->val = = Q->val && islefttreesame && isrighttreesame;    }};

Idea: or recursion, relatively slow.

http://www.waitingfy.com/archives/1588

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.