列表

详情


226. 翻转二叉树

给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。

 

示例 1:

输入:root = [4,2,7,1,3,6,9]
输出:[4,7,2,9,6,3,1]

示例 2:

输入:root = [2,1,3]
输出:[2,3,1]

示例 3:

输入:root = []
输出:[]

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * 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* invertTree(TreeNode* root) { } };

golang 解法, 执行用时: 0 ms, 内存消耗: 2.1 MB, 提交时间: 2021-07-26 14:01:52

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func invertTree(root *TreeNode) *TreeNode {
    if root == nil {
        return nil
    }

    root.Left, root.Right = root.Right, root.Left
    
    if root.Left != nil {
        invertTree(root.Left)
    }

    if root.Right != nil {
        invertTree(root.Right)
    }
    return root
}

php 解法, 执行用时: 8 ms, 内存消耗: 15 MB, 提交时间: 2021-05-10 17:38:46

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($val = 0, $left = null, $right = null) {
 *         $this->val = $val;
 *         $this->left = $left;
 *         $this->right = $right;
 *     }
 * }
 */
class Solution {

    /**
     * @param TreeNode $root
     * @return TreeNode
     */
    function invertTree(&$root) {
        if ( $root == null ) return $root;

        list($root->left, $root->right) = [$root->right, $root->left];
        if ( $root->left != null )
            $this->invertTree($root->left);
        if ( $root->right != null )
            $this->invertTree($root->right);

        return $root;
    }
}

php 解法, 执行用时: 8 ms, 内存消耗: 15.1 MB, 提交时间: 2021-05-10 17:38:32

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($val = 0, $left = null, $right = null) {
 *         $this->val = $val;
 *         $this->left = $left;
 *         $this->right = $right;
 *     }
 * }
 */
class Solution {

    /**
     * @param TreeNode $root
     * @return TreeNode
     */
    function invertTree($root) {
        if ( $root == null ) return $root;

        list($root->left, $root->right) = [$root->right, $root->left];
        if ( $root->left != null )
            $this->invertTree($root->left);
        if ( $root->right != null )
            $this->invertTree($root->right);

        return $root;
    }
}

javascript 解法, 执行用时: 92 ms, 内存消耗: 37.2 MB, 提交时间: 2020-09-16 21:22:51

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var invertTree = function(root) {
    if ( root == null ) return null;
    var temp = root.left;
    root.left = root.right;
    root.right = temp;
    if ( root.left ) {
        invertTree(root.left);
    }
    if ( root.right ) {
        invertTree(root.right);
    }
    return root;
};

golang 解法, 执行用时: 0 ms, 内存消耗: N/A, 提交时间: 2018-08-27 11:36:16

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func invertTree(root *TreeNode) *TreeNode {
    if root == nil {
        return nil
    }
    var temp *TreeNode = root.Left
    root.Left = root.Right
    root.Right = temp
    if root.Left != nil {
        invertTree(root.Left)
    }
    if root.Right != nil {
        invertTree(root.Right)
    }
    return root
}

java 解法, 执行用时: 0 ms, 内存消耗: N/A, 提交时间: 2018-08-27 11:29:54

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if ( root == null )
            return root;
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        if ( root.left != null )
            invertTree(root.left);
        if ( root.right != null )
            invertTree(root.right);
        return root;
    }
}

python3 解法, 执行用时: 40 ms, 内存消耗: N/A, 提交时间: 2018-08-27 11:24:13

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def invertTree(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        if root == None:
            return []
        root.left, root.right = root.right, root.left
        if root.left != None:
            self.invertTree(root.left)
        if root.right != None:
            self.invertTree(root.right)
        return root
            

上一题