ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
操作给定的二叉树,将其变换为源二叉树的镜像。 ~~~ 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5 ~~~ ``` /* function TreeNode(x) { this.val = x; this.left = null; this.right = null; } */ function Mirror(root) { if(root == null){ return; } if(root.left == null && root.right == null){ return; } var tempChildNode = root.left; root.left = root.right; root.right = tempChildNode; if(root.left){ Mirror(root.left); } if(root.right){ Mirror(root.right); } } ``` # 翻转二叉树 给定的二叉树,将其变换为源二叉树的镜像 ``` var invertTree = function(root) { if(root === null) return root; [root.left,root.right] = [root.right,root.left]; invertTree(root.left); invertTree(root.right); return root; }; ```