LeetCode101.Symmetric Tree

題目node

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).ide

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

    1
   / \
  2   2
   \   \
   3    3

Note:
Bonus points if you could solve it both recursively and iteratively.spa

題解code

題目的意思是,判斷一棵二叉樹是否是自我鏡像的,也就是以中心爲軸鏡面對稱。解題關鍵是肯定鏡面節點對,而後判斷該對節點值是否同樣便可。經過觀察能夠發現,只要左子樹與右子樹鏡面對稱便可,左子樹的左孩子與右子樹的右孩子配對,左子樹的右孩子與右子樹的左孩子配對,於是能夠遞歸求解 也能夠迭代求解。blog

(1)遞歸函數的輸入是配對節點,輸出是這兩個點的值是否相同。若是不相同則返回False,不然需判斷這兩個節點的左右孩子是否相同。遞歸

 1 /**
 2  * Definition for a binary tree node.
 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         if(root == NULL)
14             return true;
15         return isSymmetric(root->left,root->right);
16     }
17     bool isSymmetric(TreeNode* left,TreeNode* right)
18     {
19         if(left == NULL && right == NULL)  return true;
20         if(left == NULL || right == NULL)    return false;
21         if(left->val != right->val)
22             return false;
23         return isSymmetric(left->left,right->right) && isSymmetric(left->right,right->left);
24     }
25 };
LeetCode101_Recurively

 

(2)迭代解法:藉助兩個隊列,一個表明左子樹,一個表明右子樹,隊列中節點以此對應。隊列

 1 /**
 2  * Definition for a binary tree node.
 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         if(root == NULL)
14             return true;
15         queue<TreeNode*> lq,rq;
16         lq.push(root->left);
17         rq.push(root->right);
18         while(!lq.empty() && !rq.empty()){
19             TreeNode *tempLeft = lq.front(), * tempRight = rq.front();
20             lq.pop();rq.pop();
21             if(tempLeft == NULL){
22                 if(tempRight == NULL)
23                     continue;
24                 else
25                     return false;
26             }
27             if(tempRight == NULL)
28                 return false;
29             if(tempLeft -> val != tempRight -> val)
30                 return false;
31             lq.push(tempLeft->left);rq.push(tempRight->right);
32             lq.push(tempLeft->right);rq.push(tempRight->left);
33         }
34         if(!lq.empty())
35             return false;
36         if(!rq.empty())
37             return false;
38         return true;
39     }
40     
41 };
Leetcode101_iteratively_1

迭代的時間竟然比遞歸的時間慢了一倍。。用一個隊列也是能夠模擬的,每次從隊首取出兩個相互配對的節點便可。it

 1 /**
 2  * Definition for a binary tree node.
 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         if(root == NULL)
14             return true;
15         queue<TreeNode*> q;
16         q.push(root->left);
17         q.push(root->right);
18         while(!q.empty()){
19             TreeNode *tempLeft = q.front(); q.pop();
20             TreeNode *tempRight = q.front(); q.pop();
21             if(tempLeft == NULL && tempRight == NULL) continue;
22             if(tempLeft == NULL || tempRight == NULL) return false;
23             if(tempLeft -> val != tempRight -> val) return false;
24             q.push(tempLeft->left);
25             q.push(tempRight->right);
26             q.push(tempLeft->right);
27             q.push(tempRight->left);
28         }
29         return true;
30     }
31     
32 };
LeetCode101_iteratively_2

至於時間上變慢的緣由,目前不知。。

相關文章
相關標籤/搜索