LeetCode 226 Invert Binary Tree题解

题目地址:https://leetcode.com/problems/invert-binary-tree/

题目:

Invert a binary tree.

反转一颗二叉树

     4
   /   \
  2     7
 / \   / \
1   3 6   9
to


     4
   /   \
  7     2
 / \   / \
9   6 3   1
1)解题思路:
方法一:递归的交换左右子树
方法二:层序遍历交换顺序

2)递归法详解:
算法思想:先假定有一个函数,可以将以root为根结点的左右子树交换,并返回根结点。利用这个函数对问题进行求解:从树的根部开始,递归的过程就是,将一颗树,看作根结点和它的左子树和右子树,对左子树根结点和右子树根结点交换其左右子树。递归终止条件就是当根结点没有左右子树(即为叶结点的时候),直接返回这个节点。
Java实现:
[java]  view plain  copy
  1. /** 
  2.  * Definition for a binary tree node. 
  3.  * public class TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode left; 
  6.  *     TreeNode right; 
  7.  *     TreeNode(int x) { val = x; } 
  8.  * } 
  9.  */  
  10. public class Solution {  
  11.     public TreeNode invertTree(TreeNode root) {  
  12.         //如果为空树,直接返回  
  13.         if(root==null)  
  14.             return root;  
  15.         TreeNode temp;  
  16.         //递归结束条件  
  17.         if(root.left==null&&root.right==null)  
  18.         {  
  19.             return root;  
  20.         }  
  21.         //开始递归  
  22.         invertTree(root.left);  
  23.         invertTree(root.right);  
  24.         //回溯交换左右子树  
  25.         temp=root.right;  
  26.         root.right=root.left;  
  27.         root.left=temp;  
  28.         return root;  
  29.     }  
  30. }  

C++实现:
[cpp]  view plain  copy
  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.     TreeNode* invertTree(TreeNode* root) {  
  13.         if(root==NULL)  
  14.             return root;  
  15.         if(root->left==NULL&&root->right==NULL)  
  16.         {  
  17.             return root;  
  18.         }  
  19.         invertTree(root->left);  
  20.         invertTree(root->right);  
  21.         TreeNode *temp;  
  22.         temp=root->left;  
  23.         root->left=root->right;  
  24.         root->right=temp;  
  25.         return root;  
  26.     }  
  27. };  

3)遍历法详解:
算法思想:借助一个队列遍历序列,然后依次交换每个元素的左右子树。这次解题,我采用的是层序遍历思想。
Java实现:
[java]  view plain  copy
  1. /** 
  2.  * Definition for a binary tree node. 
  3.  * public class TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode left; 
  6.  *     TreeNode right; 
  7.  *     TreeNode(int x) { val = x; } 
  8.  * } 
  9.  */  
  10. public class Solution {  
  11.     public TreeNode invertTree(TreeNode root) {  
  12.         //如果为空树,直接返回  
  13.         if(root==null)  
  14.             return root;  
  15.         Queue <TreeNode> queue = new LinkedList<TreeNode>();  
  16.         queue.offer(root);  
  17.         while(queue.size()>0)  
  18.         {  
  19.             TreeNode newRoot=queue.poll();  
  20.             TreeNode temp = newRoot.left;  
  21.             newRoot.left=newRoot.right;  
  22.             newRoot.right=temp;  
  23.             if(newRoot.left!=null)  
  24.             {  
  25.                 queue.offer(newRoot.left);  
  26.             }  
  27.             if(newRoot.right!=null)  
  28.             {  
  29.                 queue.offer(newRoot.right);  
  30.             }  
  31.         }  
  32.         return root;  
  33.     }  
  34. }  
C++实现:
[cpp]  view plain  copy
  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.     TreeNode* invertTree(TreeNode* root) {  
  13.         if(root==NULL)  
  14.             return root;  
  15.         queue<TreeNode*>qe;  
  16.         qe.push(root);  
  17.         while(qe.size()>0)  
  18.         {  
  19.             TreeNode *newRoot = qe.front();  
  20.             qe.pop();  
  21.             TreeNode *temp = newRoot->left;  
  22.             newRoot->left = newRoot->right;  
  23.             newRoot->right = temp;  
  24.             if(newRoot->left)  
  25.             {  
  26.                 qe.push(newRoot->left);  
  27.             }  
  28.             if(newRoot->right)  
  29.             {  
  30.                 qe.push(newRoot->right);  
  31.             }  
  32.         }  
  33.         return root;  
  34.     }  
  35. };  





github: https://github.com/haoel/leetcode
转载。 https://blog.csdn.net/v_xchen_v/article/details/53071839

猜你喜欢

转载自blog.csdn.net/larry_zeng1/article/details/80360463