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