LeetCode--HOT100题(34)

目录

题目描述:94. 二叉树的中序遍历(简单)

给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。

LeetCode做题链接:LeetCode-二叉树的中序遍历

示例 1:

复制代码
输入:root = [1,null,2,3]
输出:[1,3,2]

示例 2:

复制代码
输入:root = []
输出:[]

示例 3:

复制代码
输入:root = [1]
输出:[1]

提示:

复制代码
树中节点数目在范围 [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) {

    }
}

解题思路1

递归实现:

代码

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> res = new ArrayList<>();
        dfs(res, root);
        return res;
    }

    private void dfs(List<Integer> res, TreeNode root) {
    	// 递归终止条件
        if (root == null) {
            return;
        }
        // 左 根 右
        dfs(res, root.left);
        res.add(root.val);
        dfs(res, root.right);
    }

}

解题思路2

迭代实现:

可以用栈来模拟上面的调用过程

  • 不断往左子树方向走,每走一次就将当前节点保存到栈中,模拟递归的调用
  • 当前节点为空,说明左边走到头了,从栈中弹出节点并保存
  • 然后转向右边节点,继续上面整个过程

代码

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> res = new ArrayList<Integer>();
		Stack<TreeNode> stack = new Stack<TreeNode>();
		while(stack.size()>0 || root!=null) {
			//不断往左子树方向走,每走一次就将当前节点保存到栈中
			//这是模拟递归的调用
			if(root!=null) {
				stack.add(root);
				root = root.left;
			//当前节点为空,说明左边走到头了,从栈中弹出节点并保存
			//然后转向右边节点,继续上面整个过程
			} else {
				TreeNode tmp = stack.pop();
				res.add(tmp.val);
				root = tmp.right;
			}
		}
		return res;
    }
}

成功!

PS:

感谢您的阅读!如果您觉得本篇文章对您有所帮助,请给予博主一个喔~

相关推荐
2301_8073671917 分钟前
C++中的模板方法模式
开发语言·c++·算法
PhotonixBay42 分钟前
共聚焦显微镜的结构组成与应用
人工智能·算法·机器学习
逆境不可逃1 小时前
LeetCode 热题 100 之 33. 搜索旋转排序数组 153. 寻找旋转排序数组中的最小值 4. 寻找两个正序数组的中位数
java·开发语言·数据结构·算法·leetcode·职场和发展
tankeven1 小时前
HJ137 乘之
c++·算法
add45a1 小时前
C++中的观察者模式
开发语言·c++·算法
进击的小头1 小时前
第13篇:基于伯德图的超前_滞后校正器深度设计
python·算法
leaves falling1 小时前
二分查找:迭代与递归实现全解析
数据结构·算法·leetcode
做怪小疯子2 小时前
Leetcode刷题——深度优先搜索(DFS)
算法·leetcode·深度优先
大数据AI人工智能培训专家培训讲师叶梓2 小时前
120B 数学语料 + GRPO 算法,DeepSeekMath 刷新开源大模型推理天花板
人工智能·算法·大模型·推理·deepseek·openclaw·openclaw 讲师
IMPYLH2 小时前
Linux 的 comm 命令
linux·运维·算法