遍历左孩子,将他们放进栈中,左边走到尽头,出栈,root变为栈顶元素,存值,向右边走一个
再次遍历左孩子,将他们放入栈中,如果没有左孩子了,就出栈,root变为栈顶元素,存值,向右走
public class Solution {
public IList<int> InorderTraversal(TreeNode root) {
List<int> res = new List<int>();
Stack<TreeNode> stack = new Stack<TreeNode>();
while(stack.Count != 0 || root != null)
{
while(root != null)
{
stack.Push(root);
root = root.left;
}
root = stack.Pop();
res.Add(root.val);
root = root.right;
}
return res;
}
}