Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to efficiently invert a binary tree, in Java?


/*
	This is an implementation that demonstrates
	how to efficiently invert a binary tree. 
	When inverting a binary tree, every left node 
	is swapped with its corresponding right node.

	Let n be the number of nodes of the tree and 
	h be its height.

	Time complexity: O(n) 
	Space complexity: O(h)
*/

public class InvertBinaryTree {
	private BTNode BTRoot;

	public InvertBinaryTree() {
		/*
		 * Create tree below:
		 * * 1
		 * / 
		 * 2 3
		 * * / 
		 * * 4 5
		 */
		BTRoot = new BTNode(1, null, null);
		BTNode rootLeft = new BTNode(2, null, null);
		BTRoot.left = rootLeft;
		BTNode rootRight = new BTNode(3, null, null);
		BTRoot.right = rootRight;
		BTNode rootRightLeft = new BTNode(4, null, null);
		BTNode rootRightRight = new BTNode(5, null, null);
		rootRight.left = rootRightLeft;
		rootRight.right = rootRightRight;
	}

	public static void main(String[] args) {
		InvertBinaryTree application = new InvertBinaryTree();
		application.invertBT();
		System.out.println(application.BTRoot.val); // 1
		System.out.println(application.BTRoot.left.val); // 3
		System.out.println(application.BTRoot.right.val); // 2
		System.out.println(application.BTRoot.left.left.val); // 5
		System.out.println(application.BTRoot.left.right.val); // 4
	}

	private void invertBT() {
		invertBTUtil(BTRoot);
	}

	private void invertBTUtil(BTNode tree) {
		if (tree == null)
			return;
		// Swap current left and right
		swapLeftAndRight(tree);
		// Recursively invert left subtree
		invertBTUtil(tree.left);
		// Recursively invert right subtree
		invertBTUtil(tree.right);
	}

	private void swapLeftAndRight(BTNode tree) {
		BTNode temp = tree.left;
		tree.left = tree.right;
		tree.right = temp;
	}

	// Class representing a binary tree node
	// with pointers to value, left, and right nodes
	private class BTNode {
		int val;
		BTNode left;
		BTNode right;

		public BTNode(int val, BTNode left, BTNode right) {
			this.val = val;
			this.left = left;
			this.right = right;
		}
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: taking string input in java 
Java :: programatically close a jframe 
Java :: sleep() java 
Java :: button background color not changing android 
Java :: difference between offer and add in linkedlist in java 
Java :: show confirmation dialog java android 
Java :: sum and array list java 
Java :: org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder 
Java :: java array copy 
Java :: java one line if else 
Java :: long max value java 
Java :: why python is slower than java 
Java :: main method in java without static keyword 
Java :: character.isalphanumeric java 
Java :: IntStream.generate 
Java :: check type of variable java 
Java :: java jframe button 
Java :: java eliminate numbers from string 
Java :: javafx filechooser specific file 
Java :: java.lang.NullPointerException: Cannot invoke "java.lang.CharSequence.length()" because "this.text" is null 
Java :: java for loop 
Java :: what is the difference between sc.nextLine() and sc.next() in java 
Java :: how to print two variables in same line in java 
Java :: how to read a text file in java 
Java :: change button text when clicked android studio 
Java :: how to make hello world in java 
Java :: how to loop through a 2d array java 
Java :: how to change background tint color programmatically android 
Java :: ripple effect textview android 
Java :: java define empty string list 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =