Skip to content

Instantly share code, notes, and snippets.

@nanjingdaqi
Created November 4, 2018 09:24
Show Gist options
  • Save nanjingdaqi/edb38225ac8aa556c6bf798dfbe18a57 to your computer and use it in GitHub Desktop.
Save nanjingdaqi/edb38225ac8aa556c6bf798dfbe18a57 to your computer and use it in GitHub Desktop.
public class Node {
int val;
Node left;
Node right;
}
public void preOrderScan(Node root) {
if (root == null) return;
Stack s = new Stack();
s.push(root);
while (!s.isEmpty()) {
Node cur = s.pop();
print(cur);
if (cur.right != null) s.push(cur.right);
if (cur.left != null) s.push(cur.left);
}
}
public void inOrderScan(Node root) {
if (root == null) return;
Stack s = new Stack();
Node cur = root;
While (!s.isEmpty() || cur != null) {
if (cur != null) {
s.push(cur);
cur = cur.left;
} else {
cur = s.pop();
print(cur);
cur = cur.right;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment