精选力扣500题 第64题 LeetCode 101. 对称二叉树c++/java详细题解

Posted 林深时不见鹿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了精选力扣500题 第64题 LeetCode 101. 对称二叉树c++/java详细题解相关的知识,希望对你有一定的参考价值。

1、题目

给定一个二叉树,检查它是否是镜像对称的。

例如,二叉树 [1,2,2,3,4,4,3]是对称的。

	1
   / \\
  2   2
 / \\ / \\
3  4 4  3

但是下面这个[1,2,2,null,3,null,3]则不是镜像对称的:

  1
 / \\
  2   2
   \\   \\
   3    3

进阶:

你可以运用递归和迭代两种方法解决这个问题吗?

2、思路1

(递归) O ( n ) O(n) O(n)

样例如图

递归判断两个子树是否对称。

两个子树对称当且仅当:

  1. 两个子树的根节点值相等;
  2. 第一棵子树的左子树和第二棵子树的右子树对称,且第一棵子树的右子树和第二棵子树的左子树对称;

过程如下

1、我们定义两个指针pq ,让pq指针一开始分别指向左子树和右子树。

2、同步移动这两个指针来遍历这棵树,每次检查当前 pq 节点的值是否相等,如果相等再判断左右子树是否对称。

递归边界

  • pq节点都为空时,左右子树都为空,返回true
  • pq节点只有一个为空时,左右子树不对称,返回false
  • pq节点值不相等,左右子树不对称,返回false

时间复杂度分析: 从上到下每个节点仅被遍历一遍,所以时间复杂度是 O ( n ) O(n) O(n)

3、c++代码1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(!root) return true; //根节点为空,返回true
        return dfs(root->left,root->right);//判断两棵子树是否对称
    }
    bool dfs(TreeNode* p,TreeNode* q){
        if(!p && !q) return true; //两个节点都为空
        else if (!p||!q) return false; //只有一个为空
        if(p->val != q->val) return false;
        //第一棵子树的左子树和第二棵子树的右子树对称,且第一棵子树的右子树和第二棵子树的左子树对称;
        return dfs(p->left,q->right)&&dfs(p->right,q->left);
    }
};

4、java代码1

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
         if(root == null) return true; //根节点为空,返回true
        return dfs(root.left,root.right);//判断两棵子树是否对称
    }
    public boolean dfs(TreeNode p,TreeNode q){
        if(p == null && q == null) return true; //两个节点都为空
        else if (p == null||q == null) return false; //只有一个为空
        if(p.val != q.val) return false;
        //第一棵子树的左子树和第二棵子树的右子树对称,且第一棵子树的右子树和第二棵子树的左子树对称;
        return dfs(p.left,q.right)&&dfs(p.right,q.left);
    }
}

5、思路2

(迭代) O ( n ) O(n) O(n)
用栈模拟递归,对根节点的左子树,我们用中序遍历;对根节点的右子树,我们用反中序遍历。
则两个子树互为镜像,当且仅当同时遍历两课子树时,对应节点的值相等。

时间复杂度分析: 树中每个节点仅被遍历一遍,所以时间复杂度是 O ( n ) O(n) O(n)

6、c++代码2

**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if (!root) return true;
        stack<TreeNode*> left, right;
        TreeNode *lc = root->left;
        TreeNode *rc = root->right;
        while(lc || rc || left.size())
        {
            while (lc && rc)
            {
                left.push(lc), right.push(rc);
                lc = lc->left, rc = rc->right;
            }
            if (lc || rc) return false;
            lc = left.top(), rc = right.top();
            left.pop(), right.pop();
            if (lc->val != rc->val) return false;
            lc = lc->right, rc = rc->left;
        }
        return true;
    }

};

7、java代码2

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null ) return true;
        Stack<TreeNode> left = new Stack<>();
        Stack<TreeNode> right = new Stack<>();
        TreeNode lc = root.left;
        TreeNode rc = root.right;
        while(lc != null || rc != null || left.size() != 0)
        {
            while (lc != null && rc != null)
            {
                left.push(lc);  right.push(rc);
                lc = lc.left;   rc = rc.right;
            }
            if (lc != null|| rc != null) return false;
            lc = left.peek();   rc = right.peek();
            left.pop() ; right.pop();
            if (lc.val != rc.val) return false;
            lc = lc.right;      rc = rc.left;
        }
        return true;
    }
}

原题链接: 101. 对称二叉树

在这里插入图片描述

以上是关于精选力扣500题 第64题 LeetCode 101. 对称二叉树c++/java详细题解的主要内容,如果未能解决你的问题,请参考以下文章

精选力扣500题 第10题 LeetCode 103. 二叉树的锯齿形层序遍历 c++详细题解

精选力扣500题 第20题 LeetCode 704. 二分查找c++详细题解

精选力扣500题 第8题 LeetCode 160. 相交链表 c++详细题解

精选力扣500题 第61题 LeetCode 78. 子集c++/java详细题解

精选力扣500题 第6题 LeetCode 912. 排序数组c++详细题解

精选力扣500题 第21题 LeetCode 42. 接雨水c++详细题解