`

判断一棵树是否对称(C实现)

 
阅读更多

// Type your C++ code and click the "Run Code" button!

// Your code output will be shown on the left.

// Click on the "Show input" button to enter input data to be read (from stdin).

 

#include <iostream>

using namespace std;

 

typedef int DATA;

 

typedef struct Node

{

    DATA val;

    struct Node *left;

    struct Node *right;

} TNode;

 

bool sym(TNode *l, TNode *r) {

    if(!l && !r) return true;

    if(!l || !r) return false;

    if(l->val != r->val) return false;

    return sym(l->left, r->left) && sym(l->right, r->right);

}

 

bool isSym(TNode *root) {

    if(!root) return true;

    return sym(root->left, root->right);

}

 

int main() {

    

    TNode l = {val:12, left:NULL, right:NULL};

    TNode r = {val:2, left:NULL, right:NULL};

    TNode root = {val:10, left:&l, right:&r};

    

    string result = isSym(&root)? "yes" : "no";

    

    cout << result << endl;

    

    return 0;

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics