0
点赞
收藏
分享

微信扫一扫

LC-翻转二叉树(JavaScript实现)

蓝哆啦呀 2022-02-10 阅读 44
/*
 * @lc app=leetcode.cn id=226 lang=javascript
 *
 * [226] 翻转二叉树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var invertTree = function(root) {
    if(!root) return null;
    //获取左右根节点
    const left=invertTree(root.left);
    const right=invertTree(root.right);
    //翻转
    root.left=right;
    root.right=left;
    return root;
};
// @lc code=end


举报

相关推荐

0 条评论