美文网首页
LeetCode Same Tree(非递归)

LeetCode Same Tree(非递归)

作者: codingcyx | 来源:发表于2018-04-13 16:43 被阅读0次

Given two binary trees, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

bool isSameTree(TreeNode* p, TreeNode* q) {
        queue<TreeNode*> que;
        que.push(p);
        que.push(q);
        while(!que.empty()){
            p = que.front();
            que.pop();
            q = que.front();
            que.pop();
            if(!p && !q)
                continue;
            if(!p || !q || (p -> val != q -> val))
                return false;
            que.push(p -> left);
            que.push(q -> left);
            que.push(p -> right);
            que.push(q -> right);
        }
        return true;
    }

相关文章

网友评论

      本文标题:LeetCode Same Tree(非递归)

      本文链接:https://www.haomeiwen.com/subject/roxhkftx.html