【LeetCode热题100(38/100)】翻转二叉树

题目地址: 链接
思路: 通过递归(先序遍历)遍历,交换左右两棵树位置,直到当前节点为空。

js 复制代码
/**
 * 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 root;
    [root.left, root.right] = [root.right, root.left];
    invertTree(root.left);
    invertTree(root.right);
    return root;
};
相关推荐
小欣加油3 小时前
leetcode 62 不同路径
c++·算法·leetcode·职场和发展
夏鹏今天学习了吗3 小时前
【LeetCode热题100(36/100)】二叉树的中序遍历
算法·leetcode·职场和发展
DTS小夏3 小时前
算法社Python基础入门面试题库(新手版·含答案)
python·算法·面试
Mr.Ja4 小时前
【LeetCode热题100】No.11——盛最多水的容器
算法·leetcode·贪心算法·盛水最多的容器
冷徹 .4 小时前
2024ICPC区域赛香港站
数据结构·c++·算法
浅川.255 小时前
xtuoj string
开发语言·c++·算法
韩非5 小时前
if 语句对程序性能的影响
算法·架构
用户916357440955 小时前
LeetCode热题100——15.三数之和
javascript·算法
ting_zh5 小时前
导数、偏导数与梯度:机器学习数学基础
算法·基础数学