/**
* Binary Tree Inorder Traversal
* @param {TreeNode} root
* @return {number[]}
* Inorder: left->root->right
*/
var inorderTraversal = function (root) {
let stack = [], result = [];
while (stack.length > 0 || root != null) {
if (root != null) {
stack.push(root);
root = root.left;//
} else {
root = stack.pop();//pop方法用于删除并返回数组的最后一个元素。
result.push(root);//只有inOrder才在这里push
root = root.right;
}
}
}
//Kotlin
class Solution { fun inorderTraversal(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) root = root.left } else { root = stack.pop() result.add(root.`val`) root = root.right } } return result } }