Given an integer N, the task is to print the modified binary tree pattern.
In Modified Binary Triangle Pattern, the first and last element of a Nth row are 1 and the middle (N – 2) elements are 0.
Input: N = 6
Input: N = 3
Approach: From the above examples, it can be observed that, for each row N:
- 1st and Nth element is a 1
- and elements 2 to (N-1) are 0
Therefore an algorithm can be developed to print such pattern as:
- Run a loop from 1 to N using a loop variable i, which denotes the row number of the triangle pattern.
- For each row i, run a loop from 1 to i, using a loop variable j, which denotes the number in each row.
- In each iteration of j, check if j is 1 or i. If either of it true, print 1.
- If none of the case is true for j, then print 0
- Increment the value of j by 1 after each iteration
- When the j loop has completed successfully, we have printed a row of the pattern. Therefore change the output to the next line by printing a next line.
- Increment the value of i and repeat the whole process till N rows has been printed successfully.
Below is the implementation of the above approach:
1 11 101 1001 10001 100001 1000001
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Program to print hollow Triangle pattern
- Program to print binary right angle triangle
- Print Triangle separated pattern
- Print symmetric double triangle pattern
- Program to print a Hollow Triangle inside a Triangle
- Program for incrementing/decrementing triangle pattern
- Program to print Sum Triangle for a given array
- C Program to print Floyd's triangle
- Program to print Fibonacci Triangle
- Program to print Reverse Floyd's triangle
- Program to print pattern
- Program to print the pattern "GFG"
- Program to print the pattern 'G'
- Program to print the given Z Pattern
- Program to print the given H Pattern
- Program to print the pattern ‘D’
- Sum of all the numbers present at given level in Modified Pascal’s triangle
- Program to print number pattern
- Program to print window pattern
- Program to print Swastika Pattern
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.