145. Binary Tree Postorder Traversal

/**
* 145. Binary Tree Postorder Traversal
* https://leetcode.com/problems/binary-tree-postorder-traversal/description/
* @param {TreeNode} root
* @return {number[]}
* Postorder: 左->右->根
*/
//kotlin 
fun postorderTraversal(root_: TreeNode?): List<Int>? {
        val result = arrayListOf<Int>()//read,write
        var root = root_
        val stack = Stack<TreeNode>()
        while (root != null || stack.size > 0) {
            if (root != null) {
                stack.push(root)
                result.add(0,root.`val`)
                root = root.right//loop right node
            } else {
                root = stack.pop()
                root = root.left
            }
        }
        return result
    }

 

posted @ 2019-01-01 10:51  johnny_zhao  阅读(112)  评论(0编辑  收藏  举报