//Given a binary tree, return the inorder traversal of its nodes' values.
//
//For example:
//Given binary tree {1,#,2,3},
// 1
// \
// 2
// /
// 3
//return [1,3,2].
//
//Note: Recursive solution is trivial, could you do it iteratively?
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class inorderTraversal {
public List
inorderTraversal(TreeNode root) {
List temp=new ArrayList();
if(root!=null){
temp.addAll(inorderTraversal(root.left));
temp.add(root.val);
temp.addAll(inorderTraversal(root.right));
}
return temp;
}
public List inorderTraversalNo(TreeNode root) {
List temp=new ArrayList();
Stack s=new Stack();
TreeNode p=root;
while(p!=null||!s.empty())
{
while(p!=null)
{
s.push(p);
p=p.left;
}
if(!s.empty())
{
p=s.pop();
temp.add(p.val);
p=p.right;
}
}
return temp;
}
}
阅读(349) | 评论(0) | 转发(0) |