【遍历二叉树】09判断二叉树是否关于自己镜像对称【Symmetric Tree】

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

给定一个二叉树,判断是否他自己的镜像对称的。(以自身中间,为镜像对称的)

例如羡慕这个二叉树就是对称的:

    1
   / \
  2   2
 / \ / \
3  4 4  3

但是下面这个就不是对称的:

    1
   / \
  2   2
   \   \
   3    3

笔记:

如果你既能迭代的解决这个问题,又能递归的解决这个问题,那么将给你加分。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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 recursively and iteratively. 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
test.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
 
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include "BinaryTree.h"

using namespace std;


/**
 * Definition for binary tree
 * struct TreeNode {
 * int val;
 * TreeNode *left;
 * TreeNode *right;
 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */


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

bool isSymmetric(TreeNode *root)
{
    return root ? isSymmetric(root->left, root->right) : true;
}


// 树中结点含有分叉,
//                  6
//              /       \
//             7         2
//           /   \
//          1     4
//               / \
//              3   5
int main()
{
    TreeNode *pNodeA1 = CreateBinaryTreeNode(6);
    TreeNode *pNodeA2 = CreateBinaryTreeNode(7);
    TreeNode *pNodeA3 = CreateBinaryTreeNode(2);
    TreeNode *pNodeA4 = CreateBinaryTreeNode(1);
    TreeNode *pNodeA5 = CreateBinaryTreeNode(4);
    TreeNode *pNodeA6 = CreateBinaryTreeNode(3);
    TreeNode *pNodeA7 = CreateBinaryTreeNode(5);

    ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
    ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
    ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);


    // 树中结点含有分叉,
    //                  1
    //              /       \
    //             2         2
    //           /   \       / \
    //          3     4     4   3

    TreeNode *pNodeB1 = CreateBinaryTreeNode(1);
    TreeNode *pNodeB2 = CreateBinaryTreeNode(2);
    TreeNode *pNodeB3 = CreateBinaryTreeNode(2);
    TreeNode *pNodeB4 = CreateBinaryTreeNode(3);
    TreeNode *pNodeB5 = CreateBinaryTreeNode(4);
    TreeNode *pNodeB6 = CreateBinaryTreeNode(4);
    TreeNode *pNodeB7 = CreateBinaryTreeNode(3);

    ConnectTreeNodes(pNodeB1, pNodeB2, pNodeB3);
    ConnectTreeNodes(pNodeB2, pNodeB4, pNodeB5);
    ConnectTreeNodes(pNodeB3, pNodeB6, pNodeB7);



    bool ans = isSymmetric(pNodeA1);

    if (ans == true)
    {
        cout << "Symmetric Tree!" << endl;
    }
    else
    {
        cout << "Not Symmetric Tree!" << endl;
    }

    bool ans1 = isSymmetric(pNodeB1);

    if (ans1 == true)
    {
        cout << "Symmetric Tree!" << endl;
    }
    else
    {
        cout << "Not Symmetric Tree!" << endl;
    }
    DestroyTree(pNodeA1);
    DestroyTree(pNodeB1);
    return 0;
}
结果输出:
Not Symmetric Tree!
Symmetric Tree!
 
迭代版本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
 
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include "BinaryTree.h"

using namespace std;


/**
 * Definition for binary tree
 * struct TreeNode {
 * int val;
 * TreeNode *left;
 * TreeNode *right;
 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

bool isSymmetric(TreeNode *root)
{
    if (!root)
    {
        return true;
    }
    stack<TreeNode *> s;
    s.push(root->left);
    s.push(root->right);

    TreeNode *p, *q;
    while (!s.empty())
    {
        p = s.top();
        s.pop();
        q = s.top();
        s.pop();

        if (!p && !q)
        {
            continue;
        }
        if (!p || !q)
        {
            return false;
        }
        if (p->val != q->val)
        {
            return false;
        }

        s.push(p->left);
        s.push(q->right);

        s.push(p->right);
        s.push(q->left);
    }
    return true;
}



// 树中结点含有分叉,
//                  6
//              /       \
//             7         2
//           /   \
//          1     4
//               / \
//              3   5
int main()
{
    TreeNode *pNodeA1 = CreateBinaryTreeNode(6);
    TreeNode *pNodeA2 = CreateBinaryTreeNode(7);
    TreeNode *pNodeA3 = CreateBinaryTreeNode(2);
    TreeNode *pNodeA4 = CreateBinaryTreeNode(1);
    TreeNode *pNodeA5 = CreateBinaryTreeNode(4);
    TreeNode *pNodeA6 = CreateBinaryTreeNode(3);
    TreeNode *pNodeA7 = CreateBinaryTreeNode(5);

    ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
    ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
    ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);


    // 树中结点含有分叉,
    //                  1
    //              /       \
    //             2         2
    //           /   \       / \
    //          3     4     4   3

    TreeNode *pNodeB1 = CreateBinaryTreeNode(1);
    TreeNode *pNodeB2 = CreateBinaryTreeNode(2);
    TreeNode *pNodeB3 = CreateBinaryTreeNode(2);
    TreeNode *pNodeB4 = CreateBinaryTreeNode(3);
    TreeNode *pNodeB5 = CreateBinaryTreeNode(4);
    TreeNode *pNodeB6 = CreateBinaryTreeNode(4);
    TreeNode *pNodeB7 = CreateBinaryTreeNode(3);

    ConnectTreeNodes(pNodeB1, pNodeB2, pNodeB3);
    ConnectTreeNodes(pNodeB2, pNodeB4, pNodeB5);
    ConnectTreeNodes(pNodeB3, pNodeB6, pNodeB7);



    bool ans = isSymmetric(pNodeA1);

    if (ans == true)
    {
        cout << "Symmetric Tree!" << endl;
    }
    else
    {
        cout << "Not Symmetric Tree!" << endl;
    }

    bool ans1 = isSymmetric(pNodeB1);

    if (ans1 == true)
    {
        cout << "Symmetric Tree!" << endl;
    }
    else
    {
        cout << "Not Symmetric Tree!" << endl;
    }
    DestroyTree(pNodeA1);
    DestroyTree(pNodeB1);
    return 0;
}
结果输出:
Not Symmetric Tree!
Symmetric Tree!

 
BinaryTree.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
#ifndef _BINARY_TREE_H_
#define _BINARY_TREE_H_

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};


TreeNode *CreateBinaryTreeNode(int value);
void ConnectTreeNodes(TreeNode *pParent,
                      TreeNode *pLeft, TreeNode *pRight);
void PrintTreeNode(TreeNode *pNode);
void PrintTree(TreeNode *pRoot);
void DestroyTree(TreeNode *pRoot);


#endif /*_BINARY_TREE_H_*/
BinaryTree.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 
#include <iostream>
#include <cstdio>
#include "BinaryTree.h"

using namespace std;

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */


//创建结点
TreeNode *CreateBinaryTreeNode(int value)
{
    TreeNode *pNode = new TreeNode(value);

    return pNode;
}

//连接结点
void ConnectTreeNodes(TreeNode *pParent, TreeNode *pLeft, TreeNode *pRight)
{
    if(pParent != NULL)
    {
        pParent->left = pLeft;
        pParent->right = pRight;
    }
}

//打印节点内容以及左右子结点内容
void PrintTreeNode(TreeNode *pNode)
{
    if(pNode != NULL)
    {
        printf("value of this node is: %d\n", pNode->val);

        if(pNode->left != NULL)
            printf("value of its left child is: %d.\n", pNode->left->val);
        else
            printf("left child is null.\n");

        if(pNode->right != NULL)
            printf("value of its right child is: %d.\n", pNode->right->val);
        else
            printf("right child is null.\n");
    }
    else
    {
        printf("this node is null.\n");
    }

    printf("\n");
}

//前序遍历递归方法打印结点内容
void PrintTree(TreeNode *pRoot)
{
    PrintTreeNode(pRoot);

    if(pRoot != NULL)
    {
        if(pRoot->left != NULL)
            PrintTree(pRoot->left);

        if(pRoot->right != NULL)
            PrintTree(pRoot->right);
    }
}

void DestroyTree(TreeNode *pRoot)
{
    if(pRoot != NULL)
    {
        TreeNode *pLeft = pRoot->left;
        TreeNode *pRight = pRoot->right;

        delete pRoot;
        pRoot = NULL;

        DestroyTree(pLeft);
        DestroyTree(pRight);
    }
}
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 
bool isSymmetric(TreeNode *root)
{

    if(root == NULL)
    {
        return true;
    }
    queue<TreeNode *> lt, rt;
    if(root->left != NULL)
    {
        lt.push(root->left);
    }
    if(root->right != NULL)
    {
        rt.push(root->right);
    }
    TreeNode *l;
    TreeNode *r;
    while(!lt.empty() && !rt.empty())
    {
        l = lt.front();
        lt.pop();
        r = rt.front();
        rt.pop();

        if(l == NULL && r == NULL)
        {
            continue;
        }
        if( (l != NULL && r == NULL) || (l == NULL && r != NULL) )
        {
            return false;
        }
        if(l->val != r->val)
        {
            return false;
        }
        /*这个入队列的顺序很重要*/
        lt.push(l->left);
        lt.push(l->right);
        rt.push(r->right);
        rt.push(r->left);
    }
    if(lt.empty() && rt.empty())
    {
        return true;
    }
    else
    {
        return false;
    }

}
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
bool isSameTree(TreeNode *p, TreeNode *q)
{

    if(p == NULL)
    {
        return q == NULL;
    }
    else if(q == NULL)
    {
        return false;
    }
    else if(p->val == q->val)
    {
        return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    }
    else
    {
        return false;
    }
}
posted @ 2014-04-08 16:47  z陵  阅读(364)  评论(0编辑  收藏  举报