/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
class ColorNode {
TreeNode node;
String color;
public ColorNode(TreeNode node, String color) {
this.node = node;
this.color = color;
}
}
public List<Integer> inorderTraversal(TreeNode root) {
if (root == null) return new ArrayList<Integer>();
List<Integer> res = new ArrayList<>();
Stack<ColorNode> stack = new Stack<>();
stack.push(new ColorNode(root, "white"));
while (!stack.empty()) {
ColorNode curr = stack.pop();
if (curr.color.equals("white")) {
if (curr.node.right != null)
stack.push(new ColorNode(curr.node.right, "white"));
stack.push(new ColorNode(curr.node, "gray"));
if (curr.node.left != null)
stack.push(new ColorNode(curr.node.left, "white"));
} else {
res.add(curr.node.val);
}
}
return res;
}
}
加粗样式
来源:CSDN
作者:wl1929
链接:https://blog.csdn.net/wl1929/article/details/104674969