二叉树的前序遍历、中序遍历、后序遍历怎么看呢?看中间节点的位置即可:
前序遍历:中左右
中序遍历:左中右
后序遍历:左右中
遍历二叉树也有好几种方法,最简单的是递归法(比较简单)
要学会正确运用递归函数求解问题,主要是确定三个点:①确定递归函数的输入参数和返回值;②确定递归的终止条件;③确定单层递归的逻辑。
1.二叉树前序遍历(力扣144)的递归写法如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
//使用递归进行前序遍历
List<Integer> result = new ArrayList<>();
preorder(root,result);
return result;
}
//1.确定递归函数的输入参数和返回值
public void preorder(TreeNode root, List<Integer> result){
//2.确定递归终止条件
if(root == null){
return;
}
//3.确定单层递归的逻辑
result.add(root.val);//中
preorder(root.left,result);//左
preorder(root.right,result);//右
}
}
2.二叉树中序遍历(力扣94)的递归写法如下:
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
inorder(root, res);
return res;
}
public void inorder(TreeNode root, List<Integer> list){
if(root == null){
return;
}
inorder(root.left, list);
list.add(root.val);
inorder(root.right,list);
}
}
3.二叉树后序遍历(力扣145)的递归写法如下:
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
postorder(root,res);
return res;
}
public void postorder(TreeNode root, List<Integer> list){
if(root == null){
return;
}
postorder(root.left,list);
postorder(root.right,list);
list.add(root.val);
}
}
4.二叉树前序遍历的迭代写法:
思路:使用栈来访问和使用每一个结点,先将中间结点压入栈中,然后取出中间结点的值给数组,再判断该节点下有无左右孩子,有的话,是先将右孩子给压入栈,再将左孩子压入栈,(因为访问顺序是中左右,如果先将左压入栈的话就不是左先出了)。访问时,再将左孩子给出栈,以此又重复判断左右孩子情况.....
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
//使用迭代法进行前序遍历
List<Integer> result = new ArrayList<>();
if(root == null){
return result;
}
//1.定义一个栈访问和遍历树
Stack<TreeNode> stack = new Stack<>();
//2.将根节点首先压入栈
stack.push(root);
while(!stack.isEmpty()){
//3.将栈当前的结点出栈并存入数组
TreeNode node = stack.pop();
result.add(node.val);
//4.判断当前node是否有右孩子,有则入栈
if(node.right != null){
stack.push(node.right);
}
//5.判断当前node是否有左孩子,有则入栈
if(node.left != null){
stack.push(node.left);
}
}
return result;
}
}
5.二叉树的后序遍历的迭代写法:
思路跟前序遍历类似,只不过要改变一下压栈的顺序,之前是中右左(出栈顺序为中左右),现在应该是中左右(出栈顺序是中右左)。然后再翻转结果即可
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if(root == null){
return result;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
result.add(node.val);
if(node.left != null){
stack.push(node.left);
}
if(node.right != null){
stack.push(node.right);
}
}
Collections.reverse(result);
return result;
}
}
6.二叉树的中序遍历的迭代写法:(重要)
他不同于前序和后序遍历,因为中序遍历是左中右,它的访问(遍历节点)和处理(将元素放进result数组中)无法做到同步,我们使用指针的遍历来帮助访问节点,栈则用来处理节点上的元素:
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if(root == null){
return result;
}
Stack<TreeNode> stack = new Stack<>();//栈来处理节点上的元素
TreeNode cur = root;//指针的遍历来帮助访问节点
while( cur != null || !stack.isEmpty()){
if(cur != null){
stack.push(cur);
cur = cur.left;
}else{
cur = stack.pop();
result.add(cur.val);
cur = cur.right;
}
}
return result;
}
}