Symmetric Tree [LeetCode]

Problem description: http://oj.leetcode.com/problems/symmetric-tree/

Basic idea: Both recursive and iterative solutions. 

Iterative solution:

 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) {

13         // Note: The Solution object is instantiated only once and is reused by each test case.

14         if(root == NULL) 

15             return true;

16         

17         vector<TreeNode*> layer;

18         layer.push_back(root);

19         while(true) {

20             //determine if the tree is symetric

21             for(int i = 0; i < layer.size()/2; i ++ ) {

22                 //layer[i] compare layer[layer.size() - 1 - i]

23                 int right_idx = layer.size() - 1 - i;

24                 if((layer[i]->left == NULL && layer[right_idx]->right == NULL ||

25                     (layer[i]->left != NULL && layer[right_idx]->right != NULL &&

26                     layer[i]->left->val == layer[right_idx]->right->val)) && 

27                     (layer[i]->right == NULL && layer[right_idx]->left == NULL ||

28                      (layer[i]->right != NULL && layer[right_idx]->left != NULL && 

29                      layer[i]->right->val == layer[right_idx]->left->val)))

30                     continue;

31                 else

32                     return false;

33             }

34             

35             if(layer.size() % 2 != 0) {

36                 int middle = layer.size() / 2;

37                 if(layer[middle]->left == NULL && layer[middle]->right == NULL ||

38                     (layer[middle]->left != NULL && layer[middle]->right != NULL && 

39                     layer[middle]->left->val == layer[middle]->right->val)){

40                         //do nothing

41                     }else{

42                         return false;

43                     }

44             }

45             

46             //get node for next layer

47             vector<TreeNode*> new_layer;

48             for(auto node : layer) {

49                 if(node->left != NULL)

50                     new_layer.push_back(node->left);

51                 if(node->right != NULL)

52                     new_layer.push_back(node->right);

53             }

54             if(new_layer.size() == 0)

55                 break;

56                 

57             layer = new_layer;

58         }

59         

60         return true;

61     }

62 };

Recursive solution: easier to understand.

 1 class Solution {

 2 public:

 3     bool isSubSymmetric(TreeNode * left, TreeNode * right) {

 4         if(left == NULL && right == NULL)

 5             return true;

 6         else if(left != NULL && right == NULL || (right != NULL && left == NULL))

 7             return false;

 8         else if(left->val != right->val)

 9             return false;

10         else

11             return isSubSymmetric(left->left, right->right) && isSubSymmetric(left->right, right->left);

12     }

13     bool isSymmetric(TreeNode *root) {

14         // Note: The Solution object is instantiated only once and is reused by each test case.

15         if(root == NULL) 

16             return true;

17         

18         return isSubSymmetric(root->left, root->right);

19     }

20 };

 

你可能感兴趣的:(LeetCode)