迭代
public static List<Integer> inorderTraversal(TreeNode root)
{
List<Integer> list = new ArrayList<Integer>();
Deque<TreeNode> stk = new LinkedList<TreeNode>();
TreeNode t = root;
stk.push(new TreeNode());
while (!stk.isEmpty())
{
while (t != null)
{
stk.push(t);
t = t.left;
}
TreeNode node = stk.pop();
list.add(node.val);
t = node.right;
}
list.remove(list.size()-1);
return list;
}
递归
public static List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
inorder3(root, list);
return list;
}
private static void inorder(TreeNode node, List<Integer> list) {
if (node == null)
return;
inorder(node.left, list);
list.add(node.val);
inorder(node.right, list);
}