【LeetCode】Same Tree (2 solutions)

Same Tree

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

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

解法一:递归

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
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
                return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
        }
    }
};

解法二:非递归

建立两个队列分别进行层次遍历,进队时检查对应点是否相等

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
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->left && rfront->left)
                        return false;
                    else if(lfront->left && !rfront->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)
                        ;// null
                    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;
            }
        }
    }
};

时间: 2025-01-13 02:39:01

【LeetCode】Same Tree (2 solutions)的相关文章

【LeetCode】Symmetric Tree (2 solutions)

Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note:Bonus points if you could sol

【Leetcode】Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

【LeetCode】Single Number (2 solutions)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

【Leetcode】Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 思路:使用

【Leetcode】Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

【LeetCode】Reverse Integer (2 solutions)

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread

【LeetCode】Binary Tree Right Side View 解题报告

[题目] Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example: Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3

【LeetCode】Binary Tree Maximum Path Sum 解题报告

[题目] Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. [解析] 题意:在二叉树中找一条路径,使得该路径的和最大.该路径可以从二叉树任何结点开始,也可以到任何结点结束. 思路:递归求一条经过root的最大路径,这条路径可能是:

【LeetCode】Binary Tree Inorder Traversal (2 solutions)

Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 解法一:递归 /** * Defi