二叉树的镜像 浅浅的花香味﹌ 2022-05-24 22:36 240阅读 0赞 ![这里写图片描述][70] class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } public class Solution { //方法二:精简递归方法 public void Mirror2(TreeNode root) { TreeNode tmp = null; if (root != null) { tmp = root.left; root.left = root.right; root.right = tmp; if (root.left != null) Mirror2(root.left); if (root.right != null) Mirror2(root.right); } } //方法一:一般递归方法 public void Mirror(TreeNode root) { if(root==null||(root.left==null&&root.right==null)) { return; } Switch(root); } public void Switch(TreeNode root){ if(root.left!=null&&root.right!=null){ TreeNode temp=root.left; root.left=root.right; root.right=temp; }else if(root.left==null) { root.left=root.right; root.right=null; }else if(root.right==null) { root.right=root.left; root.left=null; }else{ return; } if(root.left!=null) { Switch(root.left); } if(root.right!=null) { Switch(root.right); } } //二叉树的中序遍历 public void inOrder(TreeNode head){ if(head!=null){ inOrder(head.left); visit(head); inOrder(head.right); } } public void visit(TreeNode node){ System.out.print(node.val+" "); } public static void main(String[]args){ // System.out.println("Hello"); Solution s=new Solution(); TreeNode head=new TreeNode(8); head.left=new TreeNode(6); head.right=new TreeNode(10); head.left.left=new TreeNode(5); head.left.right=new TreeNode(7); head.right.left=new TreeNode(9); head.right.right=new TreeNode(11); TreeNode head2=new TreeNode(8); head2.left=new TreeNode(7); head2.left.left=new TreeNode(6); head2.left.left.left=new TreeNode(5); head2.left.left.left.left=new TreeNode(4); s.inOrder(head2); System.out.println(); s.Mirror(head2); s.inOrder(head2); } } ![这里写图片描述][70 1] [70]: /images/20220525/0774c83ce9fb456396b7888214f13938.png [70 1]: /images/20220525/35acbe6f60584f01be1ac4b961632559.png
相关 二叉树镜像 文章目录 题目描述 代码 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述: 二叉树的镜像定义: 源二叉树 ╰+攻爆jí腚メ/ 2024年02月19日 13:43/ 0 赞/ 160 阅读
相关 二叉树的镜像 一、前言 《剑指Offer》中题27 二、题目 请完成一个函数,输入一棵二叉树,该函数输出它的镜像。叉树节点的定义如下: ![20200517230344591 Love The Way You Lie/ 2023年03月14日 13:14/ 0 赞/ 32 阅读
相关 二叉树的镜像 剑指offer面试题19:请完成一个函数,输入一个二叉树,该函数输出它的镜像 void MirrorRecursively(BinaryTreeNode pHead) àì夳堔傛蜴生んèń/ 2022年06月17日 05:57/ 0 赞/ 246 阅读
相关 二叉树的镜像 ![这里写图片描述][70] class TreeNode { int val = 0; TreeNode left = null; 小鱼儿/ 2022年05月25日 00:04/ 0 赞/ 273 阅读
相关 二叉树的镜像 ![这里写图片描述][70] class TreeNode { int val = 0; TreeNode left = null; 浅浅的花香味﹌/ 2022年05月24日 22:36/ 0 赞/ 241 阅读
相关 二叉树的镜像 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述: 二叉树的镜像定义: 源二叉树 Love The Way You Lie/ 2022年05月14日 04:13/ 0 赞/ 282 阅读
相关 二叉树的镜像 [二叉树的镜像][Link 1] 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。 思路: 思路很明了,设置一个新结点,左右孩子交换,递归下去。 柔光的暖阳◎/ 2022年03月25日 15:26/ 0 赞/ 323 阅读
相关 二叉树的镜像 时间限制:1秒 空间限制:32768K 热度指数:221841 算法知识视频讲解 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述: 二叉树的 àì夳堔傛蜴生んèń/ 2022年03月10日 01:37/ 0 赞/ 289 阅读
还没有评论,来说两句吧...