From 55086c231acbb8794299d2f143444173de2b85e9 Mon Sep 17 00:00:00 2001 From: w2xi <43wangxi@gmail.com> Date: Sat, 2 Jul 2022 11:14:57 +0800 Subject: [PATCH] =?UTF-8?q?Update=200226.=E7=BF=BB=E8=BD=AC=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md=20JavaScript=E9=80=92=E5=BD=92=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0226.翻转二叉树.md | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index 8e35fc9d..83d20df8 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -470,25 +470,14 @@ func invertTree(root *TreeNode) *TreeNode { 使用递归版本的前序遍历 ```javascript var invertTree = function(root) { - //1. 首先使用递归版本的前序遍历实现二叉树翻转 - //交换节点函数 - const inverNode=function(left,right){ - let temp=left; - left=right; - right=temp; - //需要重新给root赋值一下 - root.left=left; - root.right=right; + // 终止条件 + if (!root) { + return null; } - //确定递归函数的参数和返回值inverTree=function(root) - //确定终止条件 - if(root===null){ - return root; - } - //确定节点处理逻辑 交换 - inverNode(root.left,root.right); - invertTree(root.left); - invertTree(root.right); + // 交换左右节点 + const rightNode = root.right; + root.right = invertTree(root.left); + root.left = invertTree(rightNode); return root; }; ```