Revering tree update (#6561)
* Add files via upload * Add files via upload * Add files via upload * Add files via upload * Add files via upload
This commit is contained in:
parent
871ee3ccce
commit
b7fbf839f9
|
@ -30,7 +30,7 @@ public class TreeNode {
|
|||
this.leftChild = leftChild;
|
||||
}
|
||||
|
||||
public TreeNode(int value, TreeNode rightChild, TreeNode leftChild) {
|
||||
public TreeNode(int value, TreeNode leftChild, TreeNode rightChild) {
|
||||
this.value = value;
|
||||
this.rightChild = rightChild;
|
||||
this.leftChild = leftChild;
|
||||
|
|
|
@ -4,21 +4,6 @@ import java.util.LinkedList;
|
|||
|
||||
public class TreeReverser {
|
||||
|
||||
public TreeNode createBinaryTree() {
|
||||
|
||||
TreeNode leaf1 = new TreeNode(3);
|
||||
TreeNode leaf2 = new TreeNode(1);
|
||||
TreeNode leaf3 = new TreeNode(9);
|
||||
TreeNode leaf4 = new TreeNode(6);
|
||||
|
||||
TreeNode nodeLeft = new TreeNode(2, leaf1, leaf2);
|
||||
TreeNode nodeRight = new TreeNode(7, leaf3, leaf4);
|
||||
|
||||
TreeNode root = new TreeNode(4, nodeRight, nodeLeft);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
public void reverseRecursive(TreeNode treeNode) {
|
||||
if (treeNode == null) {
|
||||
return;
|
||||
|
|
|
@ -10,7 +10,7 @@ public class TreeReverserUnitTest {
|
|||
public void givenTreeWhenReversingRecursivelyThenReversed() {
|
||||
TreeReverser reverser = new TreeReverser();
|
||||
|
||||
TreeNode treeNode = reverser.createBinaryTree();
|
||||
TreeNode treeNode = createBinaryTree();
|
||||
|
||||
reverser.reverseRecursive(treeNode);
|
||||
|
||||
|
@ -22,7 +22,7 @@ public class TreeReverserUnitTest {
|
|||
public void givenTreeWhenReversingIterativelyThenReversed() {
|
||||
TreeReverser reverser = new TreeReverser();
|
||||
|
||||
TreeNode treeNode = reverser.createBinaryTree();
|
||||
TreeNode treeNode = createBinaryTree();
|
||||
|
||||
reverser.reverseIterative(treeNode);
|
||||
|
||||
|
@ -30,4 +30,18 @@ public class TreeReverserUnitTest {
|
|||
.trim());
|
||||
}
|
||||
|
||||
private TreeNode createBinaryTree() {
|
||||
|
||||
TreeNode leaf1 = new TreeNode(1);
|
||||
TreeNode leaf2 = new TreeNode(3);
|
||||
TreeNode leaf3 = new TreeNode(6);
|
||||
TreeNode leaf4 = new TreeNode(9);
|
||||
|
||||
TreeNode nodeRight = new TreeNode(7, leaf3, leaf4);
|
||||
TreeNode nodeLeft = new TreeNode(2, leaf1, leaf2);
|
||||
|
||||
TreeNode root = new TreeNode(4, nodeLeft, nodeRight);
|
||||
|
||||
return root;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue