LeetCode101. Symmetric Tree

101.Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
给一个二叉树,判断它是否是中心对称二叉树。
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
image

But the following [1,2,2,null,3,null,3] is not:
image

Note:
Bonus points if you could solve it both recursively and iteratively.

public class TreeNode {     
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

因为刚做了 LeetCode.NO100 SameTree ,所以一下就想到了用相似的方法,用栈的方法,只是遍历顺序不一样,将二叉树从根开始,左右分成两棵子树,然后一棵先遍历左边,另一棵先遍历右边即可。

public boolean isSymmetric(TreeNode root) {
    if(root == null) return true;
    if(root.left == null && root.right == null) return true;
    if(root.left == null || root.right == null) return false;
    TreeNode leftTree = root.left;
    TreeNode rightTree = root.right;
    Stack<TreeNode> stack_left = new Stack<>();
    Stack<TreeNode> stack_right = new Stack<>();        
    stack_left.push(leftTree);
    stack_right.push(rightTree);
    while(!stack_left.isEmpty() && !stack_right.isEmpty()){
        TreeNode left = stack_left.pop();
        TreeNode right = stack_right.pop();
        if(left.val != right.val) return false;
        if(left.left != null) stack_left.push(left.left);
        if(right.right != null) stack_right.push(right.right);
        if(stack_left.size() != stack_right.size()) return false;
        if(left.right != null) stack_left.push(left.right);
        if(right.left != null) stack_right.push(right.left);
        if(stack_left.size() != stack_right.size()) return false;
    }       
    return stack_left.size() == stack_right.size();
}

方法二:采用递归方法

public boolean isSymmetric1(TreeNode root){
    if(root == null) return true;
    else{
        return isMirror1(root.left, root.right);
    }
}
public boolean isMirror1(TreeNode left, TreeNode right){
    if(left == null && right == null) return true;
    if(left == null || right == null) return false;
    if(left.val == right.val){
        return isMirror1(left.left, right.right) && isMirror1(left.right, right.left);
    }
    return false;
}

方法三:
跟上面方法不同就是第一次调用 isMirror 方法,isMirror2(root, root),上面的方法是判断两棵树是否是镜像树,
下面这个判断一棵树是否是镜像树。
分析时间复杂度:每个节点都会对比一次,所以总共运行次数是 O(n),n是树的节点个数。
函数递归调用次数与树的高度有关,最坏的情况,树是线性树,高度是n,因此栈的空间复杂度最坏是O(n)。

public boolean isSymmetric2(TreeNode root) {
    return isMirror2(root, root);
}
public boolean isMirror2(TreeNode t1, TreeNode t2) {
    if (t1 == null && t2 == null) return true;
    if (t1 == null || t2 == null) return false;
    return (t1.val == t2.val)
      && isMirror2(t1.right, t2.left)
      && isMirror2(t1.left, t2.right);
}

方法四:用队列的方法
用队列的方法:比较队列中相邻两个元素结构、值是否相等。
分析时间复杂度:总共运行次数是O(n),n是树的节点个数。
空间复杂度:O(n)

public boolean isSymmetric3(TreeNode root) {
    Queue<TreeNode> q = new LinkedList<>();
    q.add(root);
    q.add(root);
    while (!q.isEmpty()) {
        TreeNode t1 = q.poll();
        TreeNode t2 = q.poll();
        if (t1 == null && t2 == null) continue;
        if (t1 == null || t2 == null) return false;
        if (t1.val != t2.val) return false;
        q.add(t1.left);
        q.add(t2.right);
        q.add(t1.right);
        q.add(t2.left);
    }
    return true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值