Home [Data Sturcture] Tree Traversal (preorder, inorder, postorder)
Post
Cancel

[Data Sturcture] Tree Traversal (preorder, inorder, postorder)

Tree Traversal

Screen Shot 2023-01-09 at 2 32 07 PM

  • preorder traversal
    • A -> B -> D -> H -> I -> E -> C -> F -> G
  • inorder traversal
    • H -> D -> I -> B -> E -> A -> F -> C -> G
  • postorder traversal
    • H -> I -> D -> E -> B -> F -> G -> C -> A
  • Reference
    «https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=4717010&logNo=60209908735>

Example : preorder traversal

Problem

https://leetcode.com/problems/binary-tree-preorder-traversal/

Result

1
2
Input: root = [1,null,2,3]
Output: [1,2,3]

Answer

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
30
31
32
33
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        if (root == null) return result;

        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);

        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            result.add(node.val);
            if (node.right != null) stack.push(node.right);
            if (node.left != null) stack.push(node.left);
        }
        
        return result;
    }
}

Example : postorder traversal

Problem

https://leetcode.com/problems/binary-tree-postorder-traversal/

Result

1
2
Input: root = [1,null,2,3]
Output: [3,2,1]

Answer

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
30
31
32
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        LinkedList<Integer> result = new LinkedList<>();
        if (root == null) return result;

        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);

        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            result.addFirst(node.val); // difference
            if (node.left != null) stack.push(node.left); // difference
            if (node.right != null) stack.push(node.right); // difference
        }
        return result;
    }
}
  • Reference :

https://leetcode.com/problems/binary-tree-postorder-traversal/solutions/45556/java-simple-and-clean/?orderBy=most_votes&languageTags=java

This post is licensed under CC BY 4.0 by the author.

[Development] API vs Endpoint

[Easy] Panlindrome Linked List

Comments powered by Disqus.