File tree Expand file tree Collapse file tree 3 files changed +44
-0
lines changed
leetcode/binary-tree-postorder-traversal Expand file tree Collapse file tree 3 files changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ class TreeNode (var `val `: Int ) {
2
+ var left: TreeNode ? = null
3
+ var right: TreeNode ? = null
4
+ }
Original file line number Diff line number Diff line change
1
+ package leetcode.`binary-tree-postorder-traversal`
2
+
3
+ import TreeNode
4
+
5
+ class Solution {
6
+ fun postorderTraversal (root : TreeNode ? ): List <Int > {
7
+ val arrayList = arrayListOf<Int >()
8
+ travel(root, arrayList)
9
+ return arrayList
10
+ }
11
+
12
+ private fun travel (treeNode : TreeNode ? , list : ArrayList <Int >): List <Int > {
13
+ if (treeNode == null ) return list
14
+ // print(treeNode.`val`)
15
+ travel(treeNode.left, list)
16
+ travel(treeNode.right, list)
17
+ list.add(treeNode.`val `)
18
+ return list
19
+ }
20
+ }
Original file line number Diff line number Diff line change 1
1
package util
2
+
3
+ import ListNode
4
+ import java.util.TreeSet
5
+
6
+
7
+ fun ListNode.print () {
8
+ val set = TreeSet <ListNode >()
9
+ var exploreP: ListNode ? = this
10
+ var count = 1
11
+ while (exploreP != null ) {
12
+ print (exploreP.`val `.toString() + " " )
13
+ if (set.contains(exploreP)) {
14
+ print (" is a loop linked list,back to ${set.indexOfFirst { it == exploreP }} " )
15
+ } else {
16
+ set.add(exploreP)
17
+ }
18
+ exploreP = exploreP.next
19
+ count++
20
+ }
21
+ }
You can’t perform that action at this time.
0 commit comments