LeetCode:Symmetric Tree - 判断二叉树是否对称

1、题目名称

Symmetric Tree(判断二叉树是否对称)

2、题目地址

https://leetcode.com/problems/symmetric-tree/

3、题目内容

英文:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

中文:给定一颗二叉树,检查它是否与自己的镜像是同一棵树(即围绕根节点对称)。

4、解题方法

本题与题目“Same Tree”的解法类似,使用递归函数考察整棵树,包括左右对称节点的值和对称节点的两个子枝。需要注意的是比较子枝的时候,比较的位置是对称的位置,即用左树的右枝去比较右树的左枝,用左树的左枝去比较右树的右枝。

一段可以AC的Java代码如下:

/**
 * 功能说明:LeetCode 101 - Symmetric Tree 
 * 开发人员:Tsybius2014
 * 开发时间:2015年8月13日
 */
public class Solution {
    
    /**
     * 判断二叉树是否对称
     * @param root 二叉树根节点
     * @return true:对称 false:不对称
     */
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return isMirror(root.left, root.right);
    }
    
    /**
     * 判断一颗二叉树的左右两子节点是否对称
     * @param leftNode 左子节点 
     * @param rightNode 右子节点
     * @return true:对称 false:不对称
     */
    public boolean isMirror(TreeNode leftNode, TreeNode rightNode) {
        if (leftNode == null && rightNode == null) {
            return true;
        } else if (
            (leftNode != null && rightNode == null) ||
            (leftNode == null && rightNode != null) ||
            leftNode.val != rightNode.val ||
            !isMirror(leftNode.left, rightNode.right) ||
            !isMirror(leftNode.right, rightNode.left)) {
            return false;
        } else {
            return true;
        }
    }
}

END

时间: 2024-12-26 16:52:01

LeetCode:Symmetric Tree - 判断二叉树是否对称的相关文章

Leetcode:Symmetric Tree 判断对称树

Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 解题分析: 二叉树递归,始终是第一颗二叉树的左子树和第二颗二叉树的右

LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)

1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 bool isSymmetric(TreeNode *root) {

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 is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note:Bonus points if you could solve it both recu

【LeetCode】Symmetric Tree 判断一棵树是否是镜像的

题目:Symmetric Tree <span style="font-size:18px;">/**LeetCode Symmetric Tree 对称的树 * 思路:判断一棵树是否对称,1.有左子树就要有右子树 * 2.除根节点外对称节点值要相同 * 注意:对称后就是左子树的左节点和右子树的右节点比较 * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; *

LeetCode: Symmetric Tree [101]

[题目] Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note: Bonus points if you could solve it bot

Symmetric Tree 判断对称树

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note:Bonus points if you could solve it both recu

[leetcode]Symmetric Tree @ Python

原题地址:https://oj.leetcode.com/problems/symmetric-tree/ 题意:判断二叉树是否为对称的. Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following i

LeetCode: Symmetric Tree 解题报告

Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1   / \  2   2 / \ / \3  4 4  3But the following is not:    1   / \  2   2   \   \   3    3Note:

判断二叉树是否对称的代码

思路:要判断一颗二叉树是否对称,要判断一下几点,可以用递归来实现: 判断一颗二叉树是不是对称的,等价于判断其左右子树是不是镜像对称的 判断镜对称像即判断对称的位置上的元素是不是相等 两个节点A和B对称等价于:  这两个节点上存储的值相等 节点A的左子树节点和节点B的右子树上的节点是对称的 节点A的右子树节点和节点A的左子树上的节点是对称的 看代码: class Solution { public: bool isTreeSymmertic(TreeNode *pHead1,TreeNode *p