LeetCode 101. 对称二叉树【c++/java详细题解】

245 阅读3分钟

「这是我参与2022首次更文挑战的第18天,活动详情查看:2022首次更文挑战

1、题目

树的根节点 root , 检查它是否轴对称。

示例 1:

图片.png

 输入:root = [1,2,2,3,4,4,3]
 输出:true

示例 2:

图片.png

 输入:root = [1,2,2,null,3,null,3]
 输出:false

提示:

  • 树中节点数目在范围 [1, 1000]
  • -100 <= Node.val <= 100

2、思路

(递归) O(n)

给定我们一个二叉树的根节点root,检查这颗二叉树是否轴对称。

样例:

image-20220203230147325.png

如样例所示,root = [1,2,2,3,4,4,3],是一颗轴对称二叉树,因此我们返回true。判断一棵二叉树是否轴对称,其实就是判断这颗二叉树的左右两个子树是否互为镜像

两个子树互为镜像当且仅当:

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

image-20220203232423421.png

具体实现过程如下:

  • 1、我们定义两个指针pq ,让pq指针一开始分别指向左子树和右子树。
  • 2、同步移动这两个指针来遍历这棵树,每次检查当前 pq 节点的值是否相等,如果相等再判断左右子树是否对称。

判断两颗树对称的递归边界:

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

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

3、c++代码1

 /**
  * Definition for a binary tree node.
  * struct TreeNode {
  *     int val;
  *     TreeNode *left;
  *     TreeNode *right;
  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  * };
  */
 class Solution {
 public:
     bool isSymmetric(TreeNode* root) {
         if(!root) return true;
         return dfs(root->left, root->right);
     }
     bool dfs(TreeNode* p, TreeNode* q){
         if(!p && !q) return true;
         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)

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. 对称二叉树