一、题目
二、代码
引入栈的思想 ,沿根节点左孩子节点入栈,左边走不下去了,就打印节点,并转向右边,然后右边继续这个过程
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while(!stack.isEmpty() || root!=null){
if(root != null){
stack.push(root);
root = root.left;
}else{
TreeNode temp = stack.pop();
result.add(temp.val);
root = temp.right;
}
}
return result;
}
}