leetcode 之 Symmetric Tree 鏡像樹

來源:互聯網
上載者:User

Symmetric Tree

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
遞迴解法:由於對稱性,每次把一個節點的左子樹和它兄弟節點的左子樹進行比較,然後遞迴處理即可。
struct TreeNode {     int val;     TreeNode *left;     TreeNode *right;     TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:bool isSymmetricRecursive(TreeNode* root1,TreeNode* root2){if(root1 == NULL && root2 == NULL)return true;if(root1 == NULL || root2 == NULL)return false;return (root1->val == root2->val) && isSymmetricRecursive(root1->left,root2->right) && isSymmetricRecursive(root1->right,root2->left);}    bool isSymmetric(TreeNode *root) {        if(root == NULL)return true;return isSymmetricRecursive(root->left,root->right);    }};


迭代方法:思路和遞迴一樣,此處使用兩個隊列進行處理,把左子樹的孩子放入第一個隊列,右子樹的放入第二個隊列,每次取隊首元素進行判斷,速度比遞迴快了幾倍
struct TreeNode {     int val;     TreeNode *left;     TreeNode *right;     TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:    bool isSymmetric(TreeNode *root) {//判斷鏡像樹和產生鏡像樹不同    if(!root || !root->left && !root->right)return true;    queue<TreeNode*> leftQueue,rightQueue;    leftQueue.push(root->left);    rightQueue.push(root->right);    while(leftQueue.size() > 0 && rightQueue.size() > 0)    {    TreeNode* pNode1 = leftQueue.front();    leftQueue.pop();    TreeNode* pNode2 = rightQueue.front();    rightQueue.pop();    if(!pNode1 && !pNode2)continue;//都為空白    if(!pNode1 || !pNode2)return false;//只有一個為空白    if(pNode1->val != pNode2->val)return false;//都不為空白    leftQueue.push(pNode1->left),leftQueue.push(pNode1->right);//第一個隊列先進左子樹    rightQueue.push(pNode2->right),rightQueue.push(pNode2->left);//第二個隊列先進右子樹    }    if(leftQueue.size() > 0 || rightQueue.size() > 0)return false;    return true;    }};


劍指offer之鏡像樹  題目:輸入一顆二元尋找樹,將該樹轉換為它的鏡像,即在轉換後的二元尋找樹中,左子樹的結點都大於右子樹的結點。用遞迴和迴圈兩種方法完成樹的鏡像轉換。
分析:這裡是轉換為鏡像樹,思路和上面的判斷類似,每次分別處理左右子樹,可以使用遞迴,也可以使用迭代。此處和判斷不同的地方在於,判斷必須定位到每個節點的鏡像節點的位置,而轉換由於不需要一步轉換完成,所以每個節點之需要和相鄰節點進行交換,相對還比較簡單一點。對於迭代,可以使用隊列,也可以使用堆棧,具體代碼如下:
struct TreeNode {     int val;     TreeNode *left;     TreeNode *right;     TreeNode(int x) : val(x), left(NULL), right(NULL) {}};void SymmetricTreeRecursive(TreeNode* root)//遞迴,注意和上面判斷鏡像樹的區別{if(!root)return;TreeNode* tmp = root -> left;root -> left = root->right;root->right = tmp;SymmetricTreeRecursive(root->left);SymmetricTreeRecursive(root->right);}void SymmetricTreeIterative(TreeNode* root)//迭代{if(!root)return;stack<TreeNode*> s;s.push(root);while(!s.empty()){TreeNode* pCur = s.top();s.pop();TreeNode* tmp = pCur->left;pCur->left = pCur->right;pCur->right = tmp;if(pCur->left)s.push(pCur->left);if(pCur->right)s.push(pCur->right);}}

Same Tree  

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

遞迴解法

struct TreeNode {     int val;     TreeNode *left;     TreeNode *right;     TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:    bool isSameTree(TreeNode *p, TreeNode *q) {        if(p == NULL && q == NULL)return true;if(p == NULL || q == NULL)return false;if(p->val == q->val)return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);else return false;    }};


迭代解法(和上面類似),比遞迴演算法高了7倍
struct TreeNode {     int val;     TreeNode *left;     TreeNode *right;     TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:    bool isSameTree(TreeNode *p, TreeNode *q) {    if(!p && !q)return true;    if(!p || !q)return false;    queue<TreeNode*> queue1,queue2;    queue1.push(p),queue2.push(q);    while(queue1.size()>0 && queue2.size()>0)    {    TreeNode* pNode1 = queue1.front(),*pNode2 = queue2.front();    queue1.pop(),queue2.pop();    if(!pNode1 && !pNode2)continue;    if(!pNode1 || !pNode2)return false;    if(pNode1->val != pNode2->val)return false;    queue1.push(pNode1->left),queue1.push(pNode1->right);    queue2.push(pNode2->left),queue2.push(pNode2->right);//由於是判斷相同樹,所以進隊順序相同    }    if(queue1.size()>0 || queue2.size()>0)return false;    return true;    }};



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.