226. 翻转二叉树

226. 翻转二叉树

题目链接:226. 翻转二叉树(简单)

题目描述

翻转一棵二叉树。

示例:

输入:

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

输出:

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

题解

思路:遍历每一个节点,如果该节点的左孩子和右孩子中的一个或两个不为空,则交换位置即可。因此这道题的关键在于二叉树的遍历。这道题使用前序,后序,层次遍历都可以,二叉树的迭代遍历 的中序遍历不方便,因为中序遍历会将部分节点的左右孩子翻转两次(不过“二叉树的统一迭代遍历 ”中的中序遍历方法可以避免这个问题,因为这是用栈来实现的,而不是靠指针来遍历)。

代码(C++):

//方法一
//递归的方法:1.确定递归函数的参数和返回值;2.确定终止条件;3.确定单层递归的逻辑
class Solution1 {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (root == nullptr) return root;
        TreeNode* node = root->left;
        root->left = root->right;
        root->right = node;
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
};
​
//方法二
//深度优先遍历,栈,迭代法,标记法,前序遍历(中左右)——(右-左-中null)
class Solutio2 {
public:
    TreeNode* invertTree(TreeNode* root) {
        stack<TreeNode*> sta;
        if (root != nullptr) sta.push(root);
        while (!sta.empty()) {
            TreeNode* node = sta.top();
            if (node != nullptr) {
                sta.pop();
                if (node->right) sta.push(node->right);  //
                if (node->left) sta.push(node->left);   //
                sta.push(node);   //
                sta.push(nullptr);   //null
            } else {
                sta.pop();
                TreeNode* node1 = sta.top();
                sta.pop();
                if (node1->left != nullptr || node1->right != nullptr) {
                    TreeNode* node2 = node1->left;
                    node1->left = node1->right;
                    node1->right = node2;
                }
            }
        }
       return root;
    }
};
​
//方法三
//深度优先遍历,栈,迭代法,标记法,中序遍历(左中右)——(右-中null-左)
class Solutio3 {
public:
    TreeNode* invertTree(TreeNode* root) {
        stack<TreeNode*> sta;
        if (root != nullptr) sta.push(root);
        while (!sta.empty()) {
            TreeNode* node = sta.top();
            if (node != nullptr) {
                sta.pop();
                if (node->right) sta.push(node->right);  //
                sta.push(node);   //
                sta.push(nullptr);   //null
                if (node->left) sta.push(node->left);   //
            } else {
                sta.pop();
                TreeNode* node1 = sta.top();
                sta.pop();
                if (node1->left != nullptr || node1->right != nullptr) {
                    TreeNode* node2 = node1->left;
                    node1->left = node1->right;
                    node1->right = node2;
                }
            }
        }
        return root;
    }
};
​
//方法四
//深度优先遍历,栈,迭代法,标记法,后序遍历(左右中)——(中null-右-左)
class Solutio4 {
public:
    TreeNode* invertTree(TreeNode* root) {
        stack<TreeNode*> sta;
        if (root != nullptr) sta.push(root);
        while (!sta.empty()) {
            TreeNode* node = sta.top();
            if (node != nullptr) {
                sta.pop();
                sta.push(node);   //
                sta.push(nullptr);   //null
                if (node->right) sta.push(node->right);  //
                if (node->left) sta.push(node->left);   //
            } else {
                sta.pop();
                TreeNode* node1 = sta.top();
                sta.pop();
                if (node1->left != nullptr || node1->right != nullptr) {
                    TreeNode* node2 = node1->left;
                    node1->left = node1->right;
                    node1->right = node2;
                }
            }
        }
        return root;
    }
};
​
//方法五
//广度优先遍历,队列,迭代法,层次遍历
class Solutio5 {
public:
    TreeNode* invertTree(TreeNode* root) {
        queue<TreeNode*> que;
        if (root != nullptr) que.push(root);
        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                if (node->left != nullptr || node->right != nullptr) {
                        TreeNode* node1 = node->left;
                        node->left = node->right;
                        node->right = node1;
                }
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return root;
    }
};

代码(Java):

//方法一
//递归的方法:1.确定递归函数的参数和返回值;2.确定终止条件;3.确定单层递归的逻辑
class invertTreeSolution1 {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) return root;
        TreeNode node = root.left;
        root.left = root.right;
        root.right = node;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
}
​
//方法二(方法三,四 类似)
//深度优先遍历,栈,迭代法,标记法,前序遍历(中左右)——(右-左-中null)
class invertTreeSolution2 {
    public TreeNode invertTree(TreeNode root) {
        Stack<TreeNode> sta = new Stack<>();
        if (root != null) sta.push(root);
        while (!sta.empty()) {
            TreeNode node = sta.peek();
            if (node != null) {
                sta.pop();
                if (node.right != null) sta.push(node.right);  //
                if (node.left != null) sta.push(node.left);   //
                sta.push(node);
                sta.push(null);
            } else {
                sta.pop();
                TreeNode node1 = sta.peek();
                sta.pop();
                if (node1.left != null || node1.right != null) {
                    TreeNode node2 = node1.left;
                    node1.left = node1.right;
                    node1.right = node2;
                }
            }
        }
        return root;
    }
}
​
//方法五
//广度优先遍历,队列,迭代法,层次遍历
class invertTreeSolution5 {
    public TreeNode invertTree(TreeNode root) {
        Deque<TreeNode> que = new LinkedList<>();
        if (root != null) que.offer(root);
        while (!que.isEmpty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = que.poll();
                if (node.left != null || node.right != null) {
                    TreeNode node1 = node.left;
                    node.left = node.right;
                    node.right = node1;
                }
                if (node.left != null) que.offer(node.left);
                if (node.right != null) que.offer(node.right);
            }
        }
        return root;
    }
}

分析:

  • 时间复杂度:O(N),N是节点的个数

  • 空间复杂度:O(N)

posted @ 2021-11-28 21:59  wltree  阅读(32)  评论(0编辑  收藏  举报