LeetCode #156 Binary Tree Upside Down 上下翻轉二叉樹 156 Binary Tree Upside Down 上下翻轉二叉樹

156 Binary Tree Upside Down 上下翻轉二叉樹

Description:

Given the root of a binary tree, turn the tree upside down and return the new root.

You can turn a binary tree upside down with the following steps:

The original left child becomes the new root.
The original root becomes the new right child.
The original right child becomes the new left child.

The mentioned steps are done level by level. It is guaranteed that every right node has a sibling (a left node with the same parent) and has no children.

[圖片上傳失敗...(image-11d2f2-1667312067680)]

Example:

Example 1:

[圖片上傳失敗...(image-8308e-1667312067680)]

Input: root = [1,2,3,4,5]
Output: [4,5,2,null,null,3,1]

Example 2:

Input: root = []
Output: []

Example 3:

Input: root = [1]
Output: [1]

Constraints:

The number of nodes in the tree will be in the range [0, 10].
1 <= Node.val <= 10
Every right node in the tree has a sibling (a left node that shares the same parent).
Every right node in the tree has no children.

題目描述:

給你一個二叉樹的根節點 root ,請你將此二叉樹上下翻轉,並返回新的根節點。

你可以按下面的步驟翻轉一棵二叉樹:

原來的左子節點變成新的根節點
原來的根節點變成新的右子節點
原來的右子節點變成新的左子節點

上面的步驟逐層進行。題目數據保證每個右節點都有一個同級節點(即共享同一父節點的左節點)且不存在子節點。

[圖片上傳失敗...(image-c164cf-1667312067680)]

示例:

示例 1:

[圖片上傳失敗...(image-21b272-1667312067680)]

輸入:root = [1,2,3,4,5]
輸出:[4,5,2,null,null,3,1]

示例 2:

輸入:root = []
輸出:[]

示例 3:

輸入:root = [1]
輸出:[1]

提示:

樹中節點數目在範圍 [0, 10] 內
1 <= Node.val <= 10
樹中的每個右節點都有一個同級節點(即共享同一父節點的左節點)
樹中的每個右節點都沒有子節點

思路:

模擬
記錄最後返回的結點爲 parent 也是根結點, 這個結點實際上應該是二叉樹最左下角的結點
記錄 parent 的右結點爲 parent_right
先記錄 root 的左結點 root_left
將 parent_right 賦予 root.left, 可從示例 1 的第三層和第二層理解
然後更新 parent_right 爲 root -> right 和 root -> right 爲 parent
最後 root 向左移動
時間複雜度爲 O(n), 空間複雜度爲 O(1)

代碼:

C++:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution 
{
public:
    TreeNode* upsideDownBinaryTree(TreeNode* root) 
    {
        TreeNode *parent = nullptr, *parent_right = nullptr;
        while (root) 
        {
            TreeNode* root_left = root -> left;
            root -> left = parent_right;
            parent_right = root -> right;
            root -> right = parent;
            parent = root;
            root = root_left;
        }
        return parent;
    }
};

Java:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode upsideDownBinaryTree(TreeNode root) {
        TreeNode parent = null, parentRight = null;
        while (root != null) {
            TreeNode rootLeft = root.left;
            root.left = parentRight;
            parentRight = root.right;
            root.right = parent;
            parent = root;
            root = rootLeft;
        }
        return parent;
    }
}

Python:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def upsideDownBinaryTree(self, root: TreeNode) -> TreeNode:
        parent = parent_right = None
        while root:
            root_left = root.left
            root.left = parent_right
            parent_right = root.right
            root.right = parent
            parent = root
            root = root_left
        return parent
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章