Leetcode刷题--Week 1

本文分享了LeetCode上两道算法题的解题思路与代码实现,包括求二叉树最小深度及逆波兰表达式求值。通过深度优先搜索与广度优先搜索解决二叉树问题,并利用栈解决逆波兰表达式计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

滚动阅读全文

前言

眼瞅今年暑假就要开始找工作了,正好借助Leetcode刷题复习一下相关算法,随便为找工作做些准备。接下来每周至少回顾一个算法同时刷一道题,每周打卡。

本周刷题

题目
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

解题思路
思路一:深度优先遍历,递归遍历左右子树,遇到叶子节点时返回 1,返回过程中,比较左右子树,较小深度逐层加1。

public int run(TreeNode root) {
		if(root==null) {
			return 0;
		}
		if(root.left==null && root.right==null) {
			return 1;
		}
		int l=run(root.left);
		int r=run(root.right);
		if(l==0||r==0){ return 1+r+l;}
		return 1+Math.min(l,r);
	}

思路二:广度优先遍历,逐层遍历节点,遇到第一个叶子节点返回其深度;

public int runDFS(TreeNode root){
		if (root == null){return 0;}
		LinkedList<TreeNode> linkedList = new LinkedList<>();
		root.val = 1;
		linkedList.add(root);
		while (!linkedList.isEmpty()){
			TreeNode node = linkedList.pollFirst();
			if (node.left == null && node.right == null){return node.val;}
			if (node.left != null){
				node.left.val = node.val + 1;
				linkedList.add(node.left);
			}
			if (node.right != null ){
				node.right.val = node.val + 1;
				linkedList.add(node.right);
			}
		}
		return  0;

	}

代码:https://github.com/ZhenxueXu/leetcode/blob/master/src/solution/MinimumDepthOfBinaryTree.java

题目
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are+,-,*,/. Each operand may be an integer or another expression.

Some examples:

["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

解题思路
根据栈的原理,将数字依次入栈,遇到操作符时,从栈顶弹出两个数字,计算后将结果继续入栈;

public int evalRPN(String[] tokens) {
   	if (tokens == null || tokens.length == 0){
   		return 0;
   	}
   	Stack<Integer> value = new Stack<>();
   	int left = 0;
   	int right = 0;
   	int  result = 0;
   	for (String token : tokens) {
   		switch (token){
   			case "+":
   				right = value.pop();
   				left = value.pop();
   				result = left + right;
   				value.push(result);
   				break;
   			case "-":
   				right = value.pop();
   				left = value.pop();
   				result = left - right;
   				value.push(result);
   				break;
   			case "*":
   				right = value.pop();
   				left = value.pop();
   				result = left * right;
   				value.push(result);
   				break;
   			case "/":
   				right = value.pop();
   				left = value.pop();
   				result = left / right;
   				value.push(result);
   				break;
   				default:
   					value.push(Integer.valueOf(token));
   		}
   	}
   	return value.pop();
   }

代码:https://github.com/ZhenxueXu/leetcode/blob/master/src/solution/EvaluteReversePolishNotation.java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值