10.3 Binary Tree Reverse (Easy)

https://leetcode.com/problems/invert-binary-tree/

Use tmp to help

public class BinaryTreeReverse {

    void ReverseTree(Node root){
        if (root==null){return;}
        Node tmp = root.left;
        root.left = root.right;
        root.right = tmp;

        if(root.left != null) ReverseTree(root.left);
        if(root.right != null) ReverseTree(root.right);
    }

    public static void main(String[] args) {

        MakeBalanceTree bt = new MakeBalanceTree();
        BinaryTreeReverse reverseTree=new BinaryTreeReverse();
        BreadthFirstTraversal BFT = new BreadthFirstTraversal();

        int[] myArray = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        Node balancedTree1 = bt.makeTree(myArray);

        reverseTree.ReverseTree(balancedTree1);

        System.out.println("Print tree after Reverse");
        BFT.PrintBinaryLevelOrder(balancedTree1);
    }
}
/*
4
1 7
0 2 5 8
3 6 9
*/

results matching ""

    No results matching ""