题目描述
请实现一个函数,用来判断一棵二叉树是不是对称的。
如果一棵二叉树和它的镜像一样,那么它是对称的。
样例
如下图所示二叉树[1,2,2,3,4,4,3,null,null,null,null,null,null,null,null]为对称二叉树:
1
/ \
2 2
/ \ / \
3 4 4 3
如下图所示二叉树[1,2,2,null,4,4,3,null,null,null,null,null,null]不是对称二叉树:
1
/ \
2 2
\ / \
4 4 3
算法
(二叉树,递归) $O(n)$
递归判断两个子树是否互为镜像。
两个子树互为镜像当且仅当:
- 两个子树的根节点值相等;
- 第一棵子树的左子树和第二棵子树的右子树互为镜像,且第一棵子树的右子树和第二棵子树的左子树互为镜像;
时间复杂度
从上到下每个节点仅被遍历一遍,所以时间复杂度是 $O(n)$。
参考文献
C++ 代码
/**
* 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) {
return root==NULL||dfs(root->left,root->right);
}
bool dfs(TreeNode*p,TreeNode*q){
if(p==NULL||q==NULL)return p==NULL&&q==NULL;
return p->val==q->val&&dfs(p->left,q->right)&&dfs(p->right,q->left);
}
};
C 代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool dfs(struct TreeNode*p,struct TreeNode*q){
if(p==NULL||q==NULL)return p==NULL&&q==NULL;
return p->val==q->val&&dfs(p->left,q->right)&&dfs(p->right,q->left);
}
bool isSymmetric(struct TreeNode* root) {
return root==NULL||dfs(root->left,root->right);
}
Java 代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
return root==null||dfs(root.left,root.right);
}
public boolean dfs(TreeNode p,TreeNode q){
if(p==null||q==null)return p==null&&q==null;
return p.val==q.val&&dfs(p.left,q.right)&&dfs(p.right,q.left);
}
}
Python 代码
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
return root==None or self.dfs(root.left,root.right)
def dfs(self,p,q):
if p==None or q==None:
return p==None and q==None
return p.val==q.val and self.dfs(p.left,q.right)and self.dfs(p.right,q.left)
Javascript 代码
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var dfs=function(p,q){
if(p==null||q==null)return p==null&&q==null;
return p.val==q.val&&dfs(p.left,q.right)&&dfs(p.right,q.left);
};
var isSymmetric = function(root) {
return root==null||dfs(root.left,root.right)
};
Python3 代码
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
return root==None or self.dfs(root.left,root.right)
def dfs(self,p,q):
if(p==None or q==None):
return p==None and q==None
return p.val==q.val and self.dfs(p.left,q.right)and self.dfs(p.right,q.left)
Go 代码
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func dfs(p*TreeNode,q*TreeNode)bool{
if p==nil||q==nil{
return p==nil&&q==nil
}
return p.Val==q.Val&&dfs(p.Left,q.Right)&&dfs(p.Right,q.Left)
}
func isSymmetric(root *TreeNode) bool {
return root==nil||dfs(root.Left,root.Right)
}