中序遍历-非递归方法
class Solution { public List<Integer> inorderTraversal(TreeNode root) { //非递归的中序遍历 List<Integer> res = new ArrayList<>(); Stack<TreeNode> stack = new Stack<>(); TreeNode cur = root; while(cur!=null || !stack.isEmpty()){ while(cur!=null){ stack.push(cur); cur = cur.left; } cur = stack.pop(); res.add(cur.val); cur = cur.right; } return res; } }