- 首页 > it技术 > >
package tree.reverseTree;
import tree.Node;
import tree.myavl.BalanceTree;
/**
* Created by lijie35 on 2020-01-20 2:15 PM .
*/
public class ReverseTree {
public static void main(String[] args) {BalanceTree bl=new BalanceTree();
bl.put(10);
bl.put(5);
bl.put(15);
bl.put(4);
bl.put(7);
bl.put(11);
bl.put(16);
Node node = ReverseTree.reverseNode(bl.root);
System.out.println(node);
}/**
* 翻转树
*
*10
*/\
*515
*/ \/ \
*47 11 16
*####################
*10
*/\
*155
*/ \/ \
*16 11 74
*/
public static Node reverseNode(Node node){
if (node == null)
returnnull;
Node left = reverseNode(node.leftChild);
Node right = reverseNode(node.rightChild);
//左树变右树
node.leftChild = right;
node.rightChild = left;
returnnode;
}}
推荐阅读