前中后序遍历非递归实现
二叉树的前中后序遍历都是深度优先,而对于深度优先的遍历方式,就会和栈扯上关系;
//迭代前序遍历二叉树
public List<Character> preOrderTraversalNor(TreeNode root) {
List<Character> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.empty()) {
while (cur != null) {
stack.push(cur);
list.add(cur.value);
//System.out.print(cur.value + " ");
cur = cur.left;
}
cur = stack.pop();
cur = cur.right;
}
return list;
}
/*public void preOrderTraversalNor(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.empty()) {
while (cur != null) {
stack.push(cur);
System.out.print(cur.value + " ");
cur = cur.left;
}
cur = stack.pop();
cur = cur.right;
}
}*/
//迭代中序遍历二叉树
public List<Character> inOrderTraversalNor(TreeNode root) {
List<Character> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.empty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
list.add(cur.value);
//System.out.print(cur.value + " ");
cur = cur.right;
}
return list;
}
/*public void inOrderTraversalNor(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.empty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
System.out.print(cur.value + " ");
cur = cur.right;
}
}*/
//迭代后序遍历二叉树
public List<Character> postOrderTraversalNor(TreeNode root) {
List<Character> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
TreeNode prev = null;
while (cur != null || !stack.empty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.peek();
if (cur.right == null || cur.right == prev) {
stack.pop();
list.add(cur.value);
prev = cur;
cur = null;
}else {
cur = cur.right;
}
}
return list;
}
/*public void postOrderTraversalNor(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
TreeNode prev = null;
while (cur != null || !stack.empty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.peek();
if (cur.right == null || cur.right == prev) {
stack.pop();
System.out.print(cur.value + " ");
prev = cur;
cur = null;
}else {
cur = cur.right;
}
}
}*/
层序遍历
深度优先的遍历方式涉及到栈;而广度优先的遍历方式则涉及到队列;二叉树的层序遍历就是广度优先
//二叉树层序遍历
public List<List<Character>> levelOrder(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
List<List<Character>> ret = new ArrayList<>();
//List<Integer> list = new ArrayList<>();
if (root != null) {
queue.offer(root);
}
while (!queue.isEmpty()) {
int size = queue.size();
List<Character> list = new ArrayList<>();
while (size > 0) {
TreeNode cur = queue.poll();
//assert cur != null;
list.add(cur.value);
size--;
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
}
ret.add(list);
}
return ret;
}
/*public void levelOrder(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
if (root != null) {
queue.offer(root);
}
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
System.out.print(cur.value + " ");
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
}
}*/