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实现:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode invertTree(TreeNode root) {
        //如果为空树,直接返回
        if(root==null)
            return root;
        TreeNode temp;
        //递归结束条件
        if(root.left==null&&root.right==null)
        {
            return root;
        }
        //开始递归
        invertTree(root.left);
        invertTree(root.right);
        //回溯交换左右子树
        temp=root.right;
        root.right=root.left;
        root.left=temp;
        return root;
    }
}

C++实现:
/**
 * 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 root;
        if(root->left==NULL&&root->right==NULL)
        {
            return root;
        }
        invertTree(root->left);
        invertTree(root->right);
        TreeNode *temp;
        temp=root->left;
        root->left=root->right;
        root->right=temp;
        return root;
    }
};

3)遍历法详解:
算法思想:借助一个队列遍历序列,然后依次交换每个元素的左右子树。这次解题,我采用的是层序遍历思想。
Java实现:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode invertTree(TreeNode root) {
        //如果为空树,直接返回
        if(root==null)
            return root;
        Queue  queue = new LinkedList();
        queue.offer(root);
        while(queue.size()>0)
        {
            TreeNode newRoot=queue.poll();
            TreeNode temp = newRoot.left;
            newRoot.left=newRoot.right;
            newRoot.right=temp;
            if(newRoot.left!=null)
            {
                queue.offer(newRoot.left);
            }
            if(newRoot.right!=null)
            {
                queue.offer(newRoot.right);
            }
        }
        return root;
    }
}
C++实现:
/**
 * 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 root;
        queueqe;
        qe.push(root);
        while(qe.size()>0)
        {
            TreeNode *newRoot = qe.front();
            qe.pop();
            TreeNode *temp = newRoot->left;
            newRoot->left = newRoot->right;
            newRoot->right = temp;
            if(newRoot->left)
            {
                qe.push(newRoot->left);
            }
            if(newRoot->right)
            {
                qe.push(newRoot->right);
            }
        }
        return root;
    }
};






你可能感兴趣的:(LeetCode)