/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<Integer> inorderTraversal(TreeNode root) { //中序非递归方法 List<Integer> res=new ArrayList<Integer>(); Stack<TreeNode> st=new Stack<TreeNode>(); TreeNode temp=root; while(temp!=null||!st.isEmpty()) { // 将所有的节点都入栈,并且按照中序遍历的逆序进行入栈 if(temp==null) { temp=st.pop(); res.add(temp.val); temp=temp.right; } else { st.push(temp); temp=temp.left; } } return res; } }