Created
August 29, 2017 21:46
-
-
Save deyindra/35d8ba464d905f94b58bd4722c529166 to your computer and use it in GitHub Desktop.
TreeNode
This file contains hidden or 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
//Time O(n) and Space O(longN) | |
public static List<Pair<Integer, Integer>> getPairs(final TreeNode<Integer> tree, final int sum){ | |
List<Pair<Integer, Integer>> list = new ArrayList<>(); | |
Stack<TreeNode<Integer>> left = new Stack<>(); | |
Stack<TreeNode<Integer>> right = new Stack<>(); | |
TreeNode<Integer> leftCurrent = tree; | |
TreeNode<Integer> rightCurrent = tree; | |
while (!left.isEmpty() || !right.isEmpty() || leftCurrent!=null || rightCurrent!=null){ | |
if (leftCurrent != null || rightCurrent != null) { | |
if (leftCurrent != null) { | |
left.push(leftCurrent); | |
leftCurrent = leftCurrent.getLeft(); | |
} | |
if (rightCurrent != null) { | |
right.push(rightCurrent); | |
rightCurrent = rightCurrent.getRight(); | |
} | |
}else{ | |
TreeNode<Integer> leftValue = left.peek(); | |
TreeNode<Integer> rightValue = right.peek(); | |
int leftVal = leftValue.getData() ; | |
int rightVal = rightValue.getData(); | |
if(leftValue==rightValue){ | |
break; | |
} | |
if(leftVal + rightVal == sum){ | |
list.add(Pair.of(leftVal, rightVal)); | |
leftCurrent = left.pop(); | |
leftCurrent = leftCurrent.getRight(); | |
rightCurrent = right.pop(); | |
rightCurrent = rightCurrent.getLeft(); | |
}else if(leftVal + rightVal < sum){ | |
leftCurrent = left.pop(); | |
leftCurrent = leftCurrent.getRight(); | |
}else{ | |
rightCurrent = right.pop(); | |
rightCurrent = rightCurrent.getLeft(); | |
} | |
} | |
} | |
return list; | |
} |
This file contains hidden or 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
public class TreeNode<E> { | |
private E data; | |
private TreeNode<E> left; | |
private TreeNode<E> right; | |
public TreeNode(E data) { | |
this.data = data; | |
} | |
public void setLeft(TreeNode<E> left) { | |
this.left = left; | |
} | |
public void setRight(TreeNode<E> right) { | |
this.right = right; | |
} | |
public TreeNode<E> getLeft() { | |
return left; | |
} | |
public TreeNode<E> getRight() { | |
return right; | |
} | |
public TreeNode<E> addLeft(TreeNode<E> left){ | |
this.left = left; | |
return this; | |
} | |
public TreeNode<E> addRight(TreeNode<E> right){ | |
this.right = right; | |
return this; | |
} | |
public E getData() { | |
return data; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
TreeNode<?> treeNode = (TreeNode<?>) o; | |
return !(data != null ? !data.equals(treeNode.data) : treeNode.data != null); | |
} | |
@Override | |
public int hashCode() { | |
return data != null ? data.hashCode() : 0; | |
} | |
@Override | |
public String toString() { | |
final StringBuilder sb = new StringBuilder("TreeNode{"); | |
sb.append("data=").append(data); | |
sb.append('}'); | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment