[leetcode] 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

分析:

题目是要判断左右两颗子树是否对称,采用只要根节点的值相同,并且左边子树的左子树和右边子树饿右子树对称 且 左边子树的右子树和右边子树的左子树对称。采用树的遍历的思想,递归的解决该问题。

代码:

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root==NULL||(root->left==NULL&&root->right==NULL))
            return true;
        return postorder(root->left,root->right);
    }
    bool postorder(TreeNode* p,TreeNode* q)
    {
        if(p&&q)
        {
            if(p->val!=q->val)
                return false;
            return postorder(p->left,q->right)&&postorder(p->right,q->left);

        }else if(p==NULL&&q==NULL)
            return true;
        else
            return false;
    }
};
时间: 2024-11-05 20:14:38

[leetcode] Symmetric Tree--二叉树遍历的应用的相关文章

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

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 101 Symmetric Tree 二叉树

判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert Binary Tree,然后用Same Tree比较左右子树 而我的做法是改下Same Tree的函数,改动的是第27行 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNo

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:

Microsoft 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 [1,2,2,3,4,4,3] is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following [1,2,2,null,3,null,3] is not: 1 / 2 2 \

[Leetcode] 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 rec

LeetCode -- Symmetric Tree

Question: 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

[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

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 中文:给定一二叉树,检查它是否是它自己的镜像(比如,以中心对称). 递归: public bool