-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create Tree_Postorder_Traversal.kt #2699
Changes from 1 commit
99269f8
d5ac4aa
ec30586
ffad53f
99f1dae
07cf500
f1119d3
95c433b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import java.util.* | ||
class Node<Int>( | ||
var key:Int, | ||
var left:Node<Int>? = null, | ||
var right:Node<Int>? = null | ||
) | ||
{ | ||
fun postorderTraversal() | ||
{ | ||
left?.postorderTraversal() | ||
right?.postorderTraversal() | ||
print("$key ") | ||
} | ||
} | ||
|
||
fun main() { | ||
var read = Scanner(System.`in`) | ||
println("Enter the size of Array:") | ||
val arrSize = read.nextLine().toInt() | ||
var arr = IntArray(arrSize) | ||
val nodes = mutableListOf<Node<Int>>() | ||
println("Enter the array respresentaion of binary tree") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep a line space after this |
||
for(i in 0 until arrSize) | ||
{ | ||
arr[i] = read.nextLine().toInt() | ||
nodes.add(Node(arr[i])) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep a line space after this |
||
for(i in 0..arrSize-2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please keep space around operator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please check now? I will squash it from next time |
||
{ | ||
if((i*2)+1<arrSize && arr[(i*2)+1]!=-1) | ||
nodes[i].left = nodes[(i*2)+1] | ||
if((i*2)+2<arrSize && arr[(i*2)+2]!=-1) | ||
nodes[i].right = nodes[(i*2)+2] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep space around operators |
||
} | ||
print("Post Order traversal of tree is ") | ||
nodes[0].postorderTraversal() | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep a line space after this |
||
/* | ||
*Enter the size of Array: | ||
*7 | ||
*Enter the array respresentaion of binary tree | ||
*1 | ||
*2 | ||
*3 | ||
*4 | ||
*5 | ||
*6 | ||
*-1 | ||
*Post Order traversal of tree is 4 5 2 6 3 1 | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep a line space after this