Tuesday, 12 September 2017

Recursive Top View of Tree

Recursive Top View of Tree

  1
    \
     2
      \
       5
      /  \
     3    6
      \
       4
Top View : 1 -> 2 -> 5 -> 6

Solution:
   void topView(Node root) {
    left_view(root.left);
    System.out.print(root.data + " ");
    right_view(root.right);
    }
    void left_view(Node root) {
    if (root == null) return;
    left_view(root.left);
    System.out.print(root.data + " ");
    }
    void right_view(Node root) {
    if (root == null) return;
    System.out.print(root.data + " ");
    right_view(root.right);
    }

No comments:

Post a Comment