Day 11

Binary Tree Preorder Traversal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
preorder(root, res);
return res;
}

public void preorder(TreeNode node, List<Integer> res) {
if (node == null) {
return;
}
res.add(node.val);
preorder(node.left, res);
preorder(node.right, res);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
func preorderTraversal(root *TreeNode) []int {
var res []int
var traversal func(node *TreeNode)
traversal = func(node *TreeNode) {
if node == nil {
return
}
res = append(res, node.Val)
traversal(node.Left)
traversal(node.Right)
}
traversal(root)
return res
}

Binary Tree Postorder Traversal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
postOrder(root, res);
return res;
}

public void postOrder(TreeNode node, List<Integer> res) {
if (node == null) {
return;
}

postOrder(node.left, res);
postOrder(node.right, res);
res.add(node.val);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
func postorderTraversal(root *TreeNode) []int {
var res []int
var traversal func(node *TreeNode)
traversal = func(node *TreeNode) {
if node == nil {
return
}
traversal(node.Left)
traversal(node.Right)
res = append(res, node.Val)
}
traversal(root)
return res
}

Binary Tree Inorder Traversal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
inOrder(root, res);
return res;
}

public void inOrder(TreeNode node, List<Integer> res) {
if (node == null) {
return;
}

inOrder(node.left, res);
res.add(node.val);
inOrder(node.right, res);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
func inorderTraversal(root *TreeNode) []int {
var res []int
var traversal func(node *TreeNode)
traversal = func(node *TreeNode) {
if node == nil {
return
}
traversal(node.Left)
res = append(res, node.Val)
traversal(node.Right)
}
traversal(root)
return res
}

Binary Tree Level Order Traversal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}

Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);

while (!queue.isEmpty()) {
List<Integer> level = new ArrayList<>();
int size = queue.size();
while (size > 0) {
TreeNode node = queue.poll();
level.add(node.val);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
size--;
}
res.add(level);
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
func levelOrder(root *TreeNode) [][]int {
res := make([][]int, 0)
if root == nil {
return res
}

queue := make([]*TreeNode, 0)
queue = append(queue, root)

for len(queue) > 0 {
size := len(queue)
level := make([]int, 0)
for i := 0; i < size; i++ {
node := queue[0]
queue = queue[1:]
level = append(level, node.Val)
if node.Left != nil {
queue = append(queue, node.Left)
}
if node.Right != nil {
queue = append(queue, node.Right)
}
}
res = append(res, level)
}
return res
}