class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<Integer>(); inorder(root, res); return res; } public void inorder(TreeNode root, List<Integer> res) { if (root == null) { return; } inorder(root.left, res); res.add(root.val); inorder(root.right, res); } }
栈法
public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); if(root==null) return res; Stack<TreeNode> st = new Stack(); while( !st.isEmpty() || root!=null ) { //注意停止条件 while (root != null) { st.push(root); root = root.left; } root = st.pop(); res.add(root.val); root = root.right; } return res; }