Created
November 4, 2018 09:24
-
-
Save nanjingdaqi/edb38225ac8aa556c6bf798dfbe18a57 to your computer and use it in GitHub Desktop.
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
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