- Search for /org/gnome/deja-dup/exclude-list
- Add ['**/target', '**/.git']
martedì, novembre 13, 2018
Ubuntu deja-dup Backup [exclude list]
$ dconf-editor
lunedì, settembre 24, 2018
lunedì, gennaio 01, 2018
Invert a binary tree in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.enricogiurin.puzzles.varie; | |
/** | |
* Created by enrico on 12/30/17. | |
* https://www.programcreek.com/2014/06/leetcode-invert-binary-tree-java/ | |
*/ | |
public class BinaryTree<T extends Comparable> { | |
private BinaryTree<T> left; | |
private BinaryTree<T> right; | |
private T value; | |
public BinaryTree(T value){ | |
this.value = value; | |
} | |
public void add(T value){ | |
if(value==null){ | |
return; | |
} | |
add(value, this); | |
} | |
private void add(T value, BinaryTree binaryTree){ | |
if(value.compareTo(binaryTree.value) <= 0) { | |
//insert left | |
if(binaryTree.left == null){ | |
binaryTree.left = new BinaryTree(value); | |
}else{ | |
add(value, binaryTree.left); | |
} | |
}else{ | |
//insert right | |
if(binaryTree.right == null){ | |
binaryTree.right = new BinaryTree(value); | |
} | |
else{ | |
add(value, binaryTree.right); | |
} | |
} | |
} | |
public void printBinaryTree(BinaryTree binaryTree) { | |
System.out.println(binaryTree.value); | |
if(binaryTree.left != null){ | |
printBinaryTree(binaryTree.left); | |
} | |
if(binaryTree.right != null){ | |
printBinaryTree(binaryTree.right); | |
} | |
} | |
public void invert() { | |
invert(this); | |
} | |
private void invert(BinaryTree binaryTree){ | |
BinaryTree tmp = binaryTree.left; | |
binaryTree.left = binaryTree.right; | |
binaryTree.right = tmp; | |
if(binaryTree.left!=null){ | |
invert(binaryTree.left); | |
} | |
if(binaryTree.right!=null){ | |
invert(binaryTree.right); | |
} | |
} | |
public static void main(String[] args) { | |
BinaryTree<Integer> binaryTree = new BinaryTree<>(5); | |
binaryTree.add(2); | |
binaryTree.add(7); | |
binaryTree.add(9); | |
binaryTree.add(6); | |
binaryTree.printBinaryTree(binaryTree); | |
binaryTree.invert(); | |
System.out.println("*****"); | |
binaryTree.printBinaryTree(binaryTree); | |
} | |
} |
Iscriviti a:
Post (Atom)