二叉树的递归遍历
-
力扣链接:
144. 二叉树的前序遍历 - 力扣(LeetCode)
145. 二叉树的后序遍历 - 力扣(LeetCode)
94. 二叉树的中序遍历 - 力扣(LeetCode) -
代码随想录视频讲解链接:https://www.bilibili.com/video/BV1Wh411S7xt
/**
* 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> ret = new ArrayList<>();
//前序遍历
preorder(root, ret);
//后序遍历
postorder(root, ret);
//中序遍历
inorder(root, ret);
return ret;
}
//前序遍历
public void preorder(TreeNode root, List<Integer> ret){
if(root == null){
return;
}
ret.add(root.val);
preorder(root.left, ret);
preorder(root.right, ret);
}
//后序遍历
public void postorder(TreeNode root, List<Integer> ret){
if(root == null){
return;
}
postorder(root.left, ret);
postorder(root.right, ret);
ret.add(root.val);
}
//中序遍历
public void inorder(TreeNode root, List<Integer> ret){
if(root == null){
return;
}
inorder(root.left, ret);
ret.add(root.val);
inorder(root.right, ret);
}
}
二叉树的迭代遍历
- 代码随想录视频讲解链接:
https://www.bilibili.com/video/BV15f4y1W7i2
https://www.bilibili.com/video/BV1Zf4y1a77g
/**
* 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> ret = new ArrayList<>();
if(root == null){
return ret;
}
Stack<TreeNode> st = new Stack<>();
st.push(root);
while(!st.isEmpty()){
TreeNode tmp = st.pop();
ret.add(tmp.val);
if(tmp.right != null){
st.push(tmp.right);
}
if(tmp.left != null){
st.push(tmp.left);
}
}
return ret;
}
}
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
//前序:中左右
//后序:左右中
//后序迭代:压栈时先左后右-中右左-翻转后左右中
List<Integer> ret = new ArrayList<>();
Stack<TreeNode> st = new Stack<>();
if(root == null){
return ret;
}
st.push(root);
while(!st.isEmpty()){
TreeNode tmp = st.pop();
ret.add(tmp.val);
if(tmp.left != null){
st.push(tmp.left);
}
if(tmp.right != null){
st.push(tmp.right);
}
}
Collections.reverse(ret);
return ret;
}
}
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
//中序遍历--迭代法
List<Integer> ret = new ArrayList<>();
if(root == null){
return ret;
}
Stack<TreeNode> st = new Stack<>();
TreeNode cur = root;
while(cur != null || !st.isEmpty()){
if(cur != null){
st.push(cur);
cur = cur.left;
}else{//cur为空,叶子节点
cur = st.pop();
ret.add(cur.val);
cur = cur.right;
}
}
return ret;
}
}
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
//中序遍历--统一迭代法
List<Integer> ret = new ArrayList<>();
if(root != null){
return ret;
}
Stack<TreeNode> st = new Stack<>();
st.push(root);
while(!st.isEmpty()){
TreeNode node = st.peek();
if(node != null){
st.pop();//右中左顺序入栈,左中右出栈
if(node.right != null){
st.push(node.right);
}
st.push(node);
st.push(null);
if(node.left != null){
st.push(left);
}
}else{
st.pop();
node = st.peek();
st.pop();
ret.add(node.val);
}
}
return ret;
}