leetcode 101. Symmetric Tree

/*
leetcode 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:

1
/ \
2   2
/ \ / \
3  4 4  3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2   2
\   \
3    3

题目大意:判断给定的二叉树是不是对称的
解题思路:
1.递归:写一个函数判断两个子树是不是对称。
2.迭代,使用一个队列来存放二叉树中的节点。

*/

#include "TreeInclude.h"
#include <queue>

class Solution1 
{
public:
    bool isSymmetric(TreeNode* root)
    {
        if (!root)
            return true;
        else
            return SymmetricHelper(root->left, root->right);
    }

    bool SymmetricHelper(TreeNode* l, TreeNode* r)
    {
        if (!l && !r)
            return true;
        else if (!l || !r)
            return false;
        else if (l->val == r->val)
            return (SymmetricHelper(l->left, r->right)) && (SymmetricHelper(l->right, r->left));
        return false;
    }
};

class Solution
{
public:
    bool isSymmetric(TreeNode* root)
    {
        if (!root)
            return true;
        queue<TreeNode*> q;
        q.push(root->left);
        q.push(root->right);
        TreeNode *node1, *node2;
        while (q.size())
        {
            node1 = q.front(); q.pop();
            node2 = q.front(); q.pop();
            if (!node1 && !node2) continue;
            if (!node2 || !node1) return false;

            if (node1->val != node2->val)
                return false;
            q.push(node1->left); q.push(node2->right);
            q.push(node1->right); q.push(node2->left);
        }
        return true;
    }
};




void TEST()
{
    Solution sol;
    vector<int> v{ 1,2,2,3,4,4,3 };
    //vector<int> v{ 1,2,2,-1,3,-1,3 };
    TreeNode* root = CreateTree(v);
    cout << sol.isSymmetric(root) << endl;

}

int main()
{
    TEST();

    return 0;
}

头文件:

#ifndef _TREE_INCLUDE_H_ 
#define _TREE_INCLUDE_H_

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

// 节点数据结构
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

//使用数组创建二叉树
void CreateNode(const vector<int>& v, TreeNode* cur, int curIndex)
{
    int len = v.size();
    if (len == 0 || cur == NULL || curIndex < 0 || curIndex >= len)
        return;

    if ((2 * curIndex + 1 < len) && (v[2*curIndex+1] != -1))
    {
        TreeNode* tmp = new TreeNode(v[2 * curIndex + 1]);
        cur->left = tmp;
    }
    else
        cur->left = NULL;
    if ((2 * curIndex + 2 < len) && (v[2*curIndex+2] != -1))
    {
        TreeNode* tmp = new TreeNode(v[2 * curIndex + 2]);
        cur->right = tmp;
    }
    else
        cur->right = NULL;
    CreateNode(v, cur->left, 2 * curIndex + 1);
    CreateNode(v, cur->right, 2 * curIndex + 2);
}

TreeNode* CreateTree(const vector<int>& v)
{
    int len = v.size();
    if (len == 0)
        return NULL;

    TreeNode* root = new TreeNode(v[0]);
    CreateNode(v, root, 0);

    return root;
}

void FirstOrderTraverse(TreeNode* root)
{
    if (root == NULL)
        return;
    cout << root->val << " ";
    FirstOrderTraverse(root->left);
    FirstOrderTraverse(root->right);
    //cout << endl;
}

void MakeEmpty(TreeNode* root)
{
    if (root != NULL)
    {
        MakeEmpty(root->left);
        MakeEmpty(root->right);
        delete root;
    }
}

#endif // 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值