16.反转树(树的镜像)

package java2019; //树的镜像 public class Demo16 { public static void ReverseTree(TreeNode tree){ if(tree == null) return; //递归跳出条件 //类似递归先序遍历 TreeNode node =null; node=tree.left; tree.left = tree.right; tree.right = node; ReverseTree(tree.left); ReverseTree(tree.right); } //递归前序遍历 public static void PostOrderTravel(TreeNode node){ if(node!=null){ System.out.print(node.val); PostOrderTravel(node.left); PostOrderTravel(node.right); } } public static void main(String[] args) { TreeNode root = new TreeNode(1); TreeNode n1 = new TreeNode(2); TreeNode n2 = new TreeNode(3); TreeNode n3 = new TreeNode(4); root.left=n1; root.right=n2; n1.left=n3; ReverseTree(root); PostOrderTravel(root); }}

    推荐阅读