본문 바로가기
Programming/Data Structure

[Hackerrank] TreeInorderTraversal

by 읽고 쓰는 개발자 2020. 12. 2.

www.hackerrank.com/challenges/tree-inorder-traversal/problem

Tree: Inorder Traversal | HackerRank

Print the inorder traversal of a binary tree.

www.hackerrank.com

package exam.complete;
//https://www.hackerrank.com/challenges/tree-inorder-traversal/problem

import exam.Node;

public class TreeInorderTraversal {
    public static void main(String[] args) {
        Node node4 = new Node(4);
        Node node3 = new Node(3, null, node4);
        Node node6 = new Node(6);
        Node node5 = new Node(5, node3, node6);
        Node node2 = new Node(2, null, node5);
        Node node1 = new Node(1, null, node2);
        inOrder(node1);
    }
    public static void inOrder(Node root) {
        if(root.left != null) inOrder(root.left);
        System.out.print(root.val+" ");
        if(root.right != null) inOrder(root.right);
    }
}

'Programming > Data Structure' 카테고리의 다른 글

[Hackerrank] TreePreorderTraversal  (0) 2020.12.02
[Hackerrank] TreePostorderTraversal  (0) 2020.12.02
[Hackerrank] TreeHeightofaBinaryTree  (0) 2020.12.02
[Hackerrank] SortedInsert  (0) 2020.12.02
[Hackerrank] ReversePrint  (0) 2020.12.02