226. Invert Binary Tree

226. Invert Binary Tree

My Submissions
Question
Total Accepted: 57653  Total Submissions: 136144  Difficulty: Easy

Invert a binary tree.

     4
   /   \
  2     7
 / \   / \
1   3 6   9
to
     4
   /   \
  7     2
 / \   / \
9   6 3   1
Trivia:
This problem was inspired by  this original tweet by  Max Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.

Subscribe to see which companies asked this question

Hide Tags
  Tree




/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
 //以下是别人家的算法:自己当时实在没想清楚根据给定的代码形式如何写出反转程序
 //后序式递归算法:
class Solution {
public:
//将根节点反转,并获取翻转后该根节点的指针
     TreeNode* invertTree(TreeNode* root) {
        if(root == NULL){   
             return NULL;
        }else{
            //这样做将:树的底层先被真正交换,然后其上一层才做反转
            TreeNode* newleft = invertTree(root->right);
            TreeNode* newright = invertTree(root->left);
            root->left = newleft;
            root->right = newright;
            return root;
        }
    } 
};

其实这个给定形式应该能推测出,最终我们将二叉树反转后返回的肯定是根节点的指针,那么考虑用递归的话,每一次反转后返回的就是已经被反转后的子树的根节点,即先反转再返回那么直译就是后序式递归方法了,待我好好总结下这些该死的二叉树递归问题。


/************************************************华丽的分割线********************************************************/


多天之后再来吊打此问题,用迭代法来做:

广度优先搜索时,交换当前根节点的左右子树:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
 //思路首先:用迭代来做
 //从上往下左右子树相互交换,广度优先遍历
class Solution {
public:
    void swapNode(TreeNode* root)
    {
        TreeNode* newleft=root->right;
        TreeNode* newright=root->left;
        root->left=newleft;
        root->right=newright;
    }
    TreeNode* invertTree(TreeNode* root) {
      if(root==NULL)
        return NULL;
      //广度优先遍历交换两个子树
      queue<TreeNode*> que;  
      TreeNode* curNode=root;    
      que.push(curNode);    
      while (!que.empty())  
      {    
         curNode=que.front();//出队首元素    
         que.pop();//删除队首元素  
         swapNode(curNode);
         if(curNode->left!=NULL)  
                que.push(curNode->left);  
         if(curNode->right!=NULL)  
                que.push(curNode->right);   
      }
      return root;
    }
};


前序式深度优先搜索时,交换当前根节点的左右子树:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
 //思路首先:用迭代来做
 //从上往下左右子树相互交换,利用前序式深度优先搜索
class Solution {
public:
    void swapNode(TreeNode* root)
    {
        TreeNode* newleft=root->right;
        TreeNode* newright=root->left;
        root->left=newleft;
        root->right=newright;
    }
    TreeNode* invertTree(TreeNode* root) {
      if(root==NULL)
        return NULL;
      //前序式深度优先搜索遍历交换两个子树
      stack<TreeNode*> stk;  
      TreeNode* curNode=root;    
      stk.push(curNode);    
      while (!stk.empty())  
      {    
         curNode=stk.top();//出队首元素    
         stk.pop();//删除队首元素  
         swapNode(curNode);
         if(curNode->left!=NULL)  
                stk.push(curNode->left);  
         if(curNode->right!=NULL)  
                stk.push(curNode->right);   
      }
      return root;
    }
};


注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50421096

原作者博客:http://blog.csdn.net/ebowtang


参考资源:

【1】递归法参考博文,地址为,http://www.cnblogs.com/ganganloveu/p/4640933.html

【2】迭代法参考自己的另一篇博文:http://blog.csdn.net/ebowtang/article/details/50448037


你可能感兴趣的:(LeetCode,数据结构,算法,面试,ACM)