Binary Tree Traversals
Written By PIRATE KING
O(n)
n = size(tree)
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
def preorder(root: TreeNode) -> None: | |
if not root: return | |
print(root.val) | |
preorder(root.left) | |
preorder(root.right) |
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
def inorder(root: TreeNode) -> None: | |
if not root: return | |
inorder(root.left) | |
print(root.val) | |
inorder(root.right) |
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
def postorder(root: TreeNode) -> None: | |
if not root: return | |
postorder(root.left) | |
postorder(root.right) | |
print(root.val) |
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
void preorder(TreeNode root) { | |
if (root == null) return; | |
System.out.print(root.val + " "); | |
preorder(root.left); | |
preorder(root.right); | |
} |
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
void inorder(TreeNode root) { | |
if (root == null) return; | |
inorder(root.left); | |
System.out.print(root.val + " "); | |
inorder(root.right); | |
} |
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
void postorder(TreeNode root) { | |
if (root == null) return; | |
postorder(root.left); | |
postorder(root.right); | |
System.out.print(root.val + " "); | |
} |
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
def levelorder(root: TreeNode) -> None: | |
if not root: return | |
q = deque([root]) | |
while q: | |
size = len(q) | |
for i in range(size): | |
node = q.popleft() | |
print(str(node.val), end = " ") | |
if node.left: | |
q.append(node.left) | |
if node.right: | |
q.append(node.right) | |
print() |
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
void levelorder(TreeNode root) { | |
if (root == null) return; | |
Deque<TreeNode> q = new ArrayDeque<>(); | |
q.addLast(root); | |
while (q.isEmpty() == false) { | |
int size = q.size(); | |
for (int i = 0; i < size; i++) { | |
TreeNode node = q.removeFirst(); | |
System.out.println(node.val + " "); | |
if (node.left != null) q.addLast(node.left); | |
if (node.right != null) q.addLast(node.right); | |
} | |
System.out.println(); | |
} | |
} |