代码拉取完成,页面将自动刷新
看到java 写法中有不必要的 peek 和pop
故简化了一下写法
public List inorderTraversal(TreeNode root) {
ArrayList list = new ArrayList<>();
Stack stack = new Stack<>();
if (null != root) {
stack.push(root);
}
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
if (null != node) {
if (node.right != null) {
stack.push(node.right);
}
stack.push(node);
stack.push(null);
if (node.left != null) {
stack.push(node.left);
}
} else {
node = stack.pop();
list.add(node.val);
}
}
return list;
}