标签:镜像 树根 tree public root scribe tle mirror style
二叉树的镜像定义:源二叉树 8 / 6 10 / \ / 5 7 9 11 镜像二叉树 8 / 10 6 / \ / 11 9 7 5
*思路:交换左右子树根结点,递归处理左右子树。
1 /** 2 public class TreeNode { 3 int val = 0; 4 TreeNode left = null; 5 TreeNode right = null; 6 7 public TreeNode(int val) { 8 this.val = val; 9 10 } 11 12 } 13 */ 14 public class Solution { 15 public void Mirror(TreeNode root) { 16 TreeNode tempNode = null; 17 if(root!=null){ 18 tempNode = root.left; 19 root.left = root.right; 20 root.right = tempNode; 21 Mirror(root.left); 22 Mirror(root.right); 23 } 24 } 25 26 }
标签:镜像 树根 tree public root scribe tle mirror style
原文地址:https://www.cnblogs.com/singular/p/10045800.html