Problem: 144. 二叉树的先序遍历
Problem: 94. 二叉树的中序遍历
Problem: 145. 二叉树的后序遍历
思路
递归实现
迭代实现
递归-先序遍历
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
preOrder(root, list);
return list;
}
/**
* 递归函数
*/
public void preOrder(TreeNode root, List<Integer> list) {
//终止条件
if (root == null) {
return;
}
list.add(root.val);
preOrder(root.left, list);
preOrder(root.right,list);
}
}
递归-中序遍历
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
inOrder(root, list);
return list;
}
/**
* 递归函数
*/
public void inOrder(TreeNode root, List<Integer> list) {
//终止条件
if (root == null) {
return;
}
inOrder(root.left, list);
list.add(root.val);
inOrder(root.right,list);
}
}
递归-后序遍历
class Solution{
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
postOrder(root, list);
return list;
}
/**
* 递归函数
*/
public void postOrder(TreeNode root, List<Integer> list) {
//终止条件
if (root == null) {
return;
}
postOrder(root.left, list);
postOrder(root.right,list);
list.add(root.val);
}
}
非递归 先序遍历
/**
* 非递归 实现 二叉树的 先序遍历
*
* 前序遍历顺序:中-左-右,
* 入栈顺序:中-右-左
* 出栈顺序:中-左-右
*/
class SolutionPreorder2 {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) {
return list;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
list.add(node.val);
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
return list;
}
}
非递归-中序遍历
/**
* 非递归 二叉树的中序遍历
* 中序:左-中-右
*
*/
class Solutioninorder2 {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (list == null) {
return list;
}
Stack<TreeNode> stack = new Stack<>();
//中序遍历 需要借助指针
TreeNode prev = root;
while (!stack.isEmpty() || prev != null) {
if (prev != null) {
stack.push(prev);
prev = prev.left;
}else {
prev = stack.pop();
list.add(prev.val);
prev = prev.right;
}
}
return list;
}
}
非递归-后序遍历
/**
* 非递归 二叉树的后序遍历
* 后序 左右中
* 入栈顺序 中左右
* 出栈顺序 中右左
* 翻转 左右中
*/
class SolutionPostorder2 {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) {
return list;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
list.add(node.val);
if (node.left != null) {
stack.push(node.left);
}
if (node.right != null) {
stack.push(node.right);
}
}
// 翻转
Collections.reverse(list);
return list;
}
}
非递归-统一写法
中序
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if (root != null) {
stack.push(root);
}
while (!stack.isEmpty()) {
TreeNode node = stack.peek();
if (node != null) {
stack.pop();
if (node.right != null) {
stack.push(node.right);
}
stack.push(node);
stack.push(null);
if (node.left != null) {
stack.push(node.left);
}
}else {
stack.pop();
node = stack.peek();
stack.pop();
list.add(node.val);
}
}
return list;
}
}
先序遍历
/**
* 非递归 统一写法 前序遍历
*/
class Solutionalla{
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if (root != null) {
stack.push(root);
}
while (!stack.isEmpty()) {
TreeNode node = stack.peek();
if (node != null) {
stack.pop();
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
stack.push(node);
stack.push(null);
}else {
stack.pop();
node = stack.peek();
stack.pop();
list.add(node.val);
}
}
return list;
}
}
后续遍历
/**
* 非递归 统一写法 后序遍历
*/
class Solutionallaa{
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if (root != null) {
stack.push(root);
}
while (!stack.isEmpty()) {
TreeNode node = stack.peek();
if (node != null) {
stack.pop();
stack.push(node);
stack.push(null);
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}else {
stack.pop();
node = stack.peek();
stack.pop();
list.add(node.val);
}
}
return list;
}
}