Easy-题目25:101. Symmetric Tree

来源:互联网 发布:金山软件估值 编辑:程序博客网 时间:2024/05/17 03:37

题目原文:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
题目大意:
给一个二叉树,判断它是不是沿中线轴对称的。
题目分析:
(1) 空树是轴对称的。
(2) 轴对称的两棵子树tree1和tree2应满足以下递归条件:
a) 对应值相等;
b) Tree1的右子树和tree2的左子树是轴对称的;
c) Tree1的左子树和tree2的右子树的轴对称的。
源码:(language:java)

public class Solution {    public boolean isSymmetric(TreeNode root) {        if(root== null)             return true;        return isSymmetric(root.left, root.right);            }    public boolean isSymmetric(TreeNode tree1, TreeNode tree2){        if(tree1==null && tree2==null)            return true;        else if(tree1 == null || tree2 == null || tree1.val != tree2.val)            return false;        else            return isSymmetric(tree1.left, tree2.right) && isSymmetric(tree1.right, tree2.left);    }}

成绩:
1ms,beats21.41%,众数1ms,78.59%
Cmershen的碎碎念:
这道题很基础,但当时却没有想到。这种题应该予以重视,以免面试时面对如此简单题却无能为力。

0 0
原创粉丝点击