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

阅读 44

2022-02-10

/*
 * @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)

0 0 举报