递归解法
var result = []
function inorderTraversal( root ) {
// write code here
if(!root) {return []}
inorderTraversal(root.left)
result.push(root.val)
inorderTraversal(root.right)
return result
}
迭代解法
/*
* function TreeNode(x) {
* this.val = x;
* this.left = null;
* this.right = null;
* }
*/
/**
*
* @param root TreeNode类
* @return int整型一维数组
*/
function inorderTraversal( root ) {
// write code here
var stack = []
var result = []
var current = root
while(current || stack.length){
if(current){
stack.push(current)
current = current.left
}else{
current = stack.pop()
result.push(current.val)
current = current.right
}
}
return result
}
module.exports = {
inorderTraversal : inorderTraversal
};