1 递归遍历
递归算法三要素:
-
确定递归函数的参数和返回值: 确定哪些参数是递归的过程中需要处理的,那么就在递归函数里加上这个参数, 并且还要明确每次递归的返回值是什么进而确定递归函数的返回类型。
-
确定终止条件: 写完了递归算法, 运行的时候,经常会遇到栈溢出的错误,就是没写终止条件或者终止条件写的不对,操作系统也是用一个栈的结构来保存每一层递归的信息,如果递归没有终止,操作系统的内存栈必然就会溢出。
-
确定单层递归的逻辑: 确定每一层递归需要处理的信息。在这里也就会重复调用自己来实现递归的过程。
二叉树遍历方式:
1.深度优先遍历:前序遍历、中序遍历、后序遍历【顺便把二叉树数据结构知识重新温习一下】
(1)前序遍历:遍历二叉树时按照中,左,右结点依次读取结点值,读取顺序和遍历顺序一致;
(2)中序遍历:遍历二叉树时按照左、中、右结点依次读取结点值;
(3)后续遍历:遍历二叉树时按照左、右、中结点依次读取结点值,逻辑思维与前序一致。
2.广度优先遍历:层序遍历
二叉树定义:个人认为结点定义类似于链表结点定义的一种形式
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;
}
}
2 迭代遍历
迭代法(非递归法):用栈实现递归函数。
注意:
1.用迭代法遍历时,前序和后序需要注意入栈和出栈顺序;
2.中序遍历用迭代法与前序和后序逻辑不同,因为读取顺序和数值存储顺序不同。
3 144. 二叉树的前序遍历
题目: 给你二叉树的根节点 root
,返回它节点值的 前序 遍历。
提示:
- 树中节点数目在范围
[0, 100]
内 -100 <= Node.val <= 100
Java实现
/**
* 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<Integer>();
// preorder(root,result);
// return result;
// 迭代法(非递归),栈结构,入栈为中右左,出栈才为中左右
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.right!=null){
stack.push(node.right);
}
if(node.left!=null){
stack.push(node.left);
}
}
return result;
}
// 递归实现
// public void preorder(TreeNode root,List<Integer> result){
// if(root==null){
// return;
// }
// result.add(root.val);
// preorder(root.left,result);
// preorder(root.right,result);
// }
}
4 145. 二叉树的后序遍历
题目: 给你一棵二叉树的根节点 root
,返回其节点值的 后序遍历 。
提示:
- 树中节点的数目在范围
[0, 100]
内 -100 <= Node.val <= 100
Java实现
/**
* 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> postorderTraversal(TreeNode root) {
// 递归法
// List<Integer> result = new ArrayList<Integer>();
// postorder(root,result);
// return result;
// 迭代法(非递归),与前序遍历形式一致
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;
}
// 递归法
// public void postorder(TreeNode root,List<Integer> result){
// if(root==null){
// return;
// }
// postorder(root.left,result);
// postorder(root.right,result);
// result.add(root.val);
// }
}
5 94. 二叉树的中序遍历
题目: 给定一个二叉树的根节点 root
,返回 它的 中序 遍历 。
提示:
- 树中节点数目在范围
[0, 100]
内 -100 <= Node.val <= 100
Java实现
/**
* 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> inorderTraversal(TreeNode root) {
// 递归法
// List<Integer> result = new ArrayList<Integer>();
// inorder(root,result);
// return result;
// 迭代法(非递归),与前序后序遍历不同,因为读取顺序和存储顺序不同
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;
}
// 递归法
// public void inorder(TreeNode root,List<Integer> result){
// if(root==null){
// return;
// }
// inorder(root.left,result);
// result.add(root.val);
// inorder(root.right,result);
// }
}