5.30 树
This commit is contained in:
parent
60e38e3d55
commit
5b858edaa8
41
src/main/java/stack/RemoveDuplicates.java
Normal file
41
src/main/java/stack/RemoveDuplicates.java
Normal file
@ -0,0 +1,41 @@
|
||||
package stack;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
|
||||
/**
|
||||
* 题目: 1047. 删除字符串中的所有相邻重复项 ( removeDuplicates)
|
||||
* 描述:给出由小写字母组成的字符串 s,重复项删除操作会选择两个相邻且相同的字母,并删除它们。
|
||||
* 在 s 上反复执行重复项删除操作,直到无法继续删除。
|
||||
* 在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。
|
||||
*
|
||||
* 示例 1:
|
||||
输入:"abbaca"
|
||||
输出:"ca"
|
||||
解释:例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。
|
||||
之后我们得到字符串 "aaca",其中又只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"。
|
||||
|
||||
* 链接:https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/
|
||||
*/
|
||||
public class RemoveDuplicates {
|
||||
public String removeDuplicates(String s) {
|
||||
Deque<Character>stack=new ArrayDeque<>();
|
||||
stack.push(s.charAt(0));
|
||||
for (int i = 1; i < s.length(); i++) {
|
||||
if(!stack.isEmpty()) {
|
||||
char top = stack.peek();
|
||||
if (top == s.charAt(i))
|
||||
stack.pop();
|
||||
else
|
||||
stack.push(s.charAt(i));
|
||||
}
|
||||
else
|
||||
stack.push(s.charAt(i));
|
||||
}
|
||||
StringBuilder sb=new StringBuilder();
|
||||
while (!stack.isEmpty()){
|
||||
sb.insert(0,stack.pop());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
39
src/main/java/tree/BinaryTreePaths.java
Normal file
39
src/main/java/tree/BinaryTreePaths.java
Normal file
@ -0,0 +1,39 @@
|
||||
package tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 题目: 257. 二叉树的所有路径 ( binaryTreePaths)
|
||||
* 描述:给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。
|
||||
* 叶子节点 是指没有子节点的节点。
|
||||
*
|
||||
* 示例 1:
|
||||
输入:root = [1,2,3,null,5]
|
||||
输出:["1->2->5","1->3"]
|
||||
|
||||
* 链接:https://leetcode.cn/problems/binary-tree-paths/
|
||||
*/
|
||||
public class BinaryTreePaths {
|
||||
public List<String> binaryTreePaths(TreeNode root) {
|
||||
List<String> res = new ArrayList<>();
|
||||
if (root == null) return res;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
dfs(root, sb, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
private void dfs(TreeNode node, StringBuilder sb, List<String> res) {
|
||||
int len = sb.length(); // 记录进入当前节点前的长度
|
||||
sb.append(node.val);
|
||||
// 如果是叶子节点
|
||||
if (node.left == null && node.right == null) {
|
||||
res.add(sb.toString());
|
||||
} else {
|
||||
sb.append("->");
|
||||
if (node.left != null) dfs(node.left, sb, res);
|
||||
if (node.right != null) dfs(node.right, sb, res);
|
||||
}
|
||||
sb.setLength(len); // 回溯:恢复到进入本节点前的状态
|
||||
}
|
||||
}
|
47
src/main/java/tree/ConstructMaximumBinaryTree.java
Normal file
47
src/main/java/tree/ConstructMaximumBinaryTree.java
Normal file
@ -0,0 +1,47 @@
|
||||
package tree;
|
||||
/**
|
||||
* 题目: 654. 最大二叉树 (constructMaximumBinaryTree )
|
||||
* 描述:给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建:
|
||||
*
|
||||
* 创建一个根节点,其值为 nums 中的最大值。
|
||||
* 递归地在最大值 左边 的 子数组前缀上 构建左子树。
|
||||
* 递归地在最大值 右边 的 子数组后缀上 构建右子树。
|
||||
* 返回 nums 构建的 最大二叉树 。
|
||||
*
|
||||
* 示例 1:
|
||||
输入:nums = [3,2,1,6,0,5]
|
||||
输出:[6,3,5,null,2,0,null,null,1]
|
||||
解释:递归调用如下所示:
|
||||
- [3,2,1,6,0,5] 中的最大值是 6 ,左边部分是 [3,2,1] ,右边部分是 [0,5] 。
|
||||
- [3,2,1] 中的最大值是 3 ,左边部分是 [] ,右边部分是 [2,1] 。
|
||||
- 空数组,无子节点。
|
||||
- [2,1] 中的最大值是 2 ,左边部分是 [] ,右边部分是 [1] 。
|
||||
- 空数组,无子节点。
|
||||
- 只有一个元素,所以子节点是一个值为 1 的节点。
|
||||
- [0,5] 中的最大值是 5 ,左边部分是 [0] ,右边部分是 [] 。
|
||||
- 只有一个元素,所以子节点是一个值为 0 的节点。
|
||||
- 空数组,无子节点。
|
||||
|
||||
* 链接:https://leetcode.cn/problems/maximum-binary-tree/
|
||||
*/
|
||||
public class ConstructMaximumBinaryTree {
|
||||
int findmax(int[] nums,int l, int r){
|
||||
int MaxIndex=l;
|
||||
for (int i = l+1; i <= r; i++) {
|
||||
if(nums[MaxIndex]<nums[i])
|
||||
MaxIndex=i;
|
||||
}
|
||||
return MaxIndex;
|
||||
}
|
||||
TreeNode dfs(int[] nums,int l,int r){
|
||||
if(l>r)return null;
|
||||
int maxindex=findmax(nums,l,r);
|
||||
TreeNode root=new TreeNode(nums[maxindex]);
|
||||
root.left=dfs(nums,l,maxindex-1);
|
||||
root.right=dfs(nums,maxindex+1,r);
|
||||
return root;
|
||||
}
|
||||
public TreeNode constructMaximumBinaryTree(int[] nums) {
|
||||
return dfs(nums,0,nums.length-1);
|
||||
}
|
||||
}
|
40
src/main/java/tree/FindBottomLeftValue.java
Normal file
40
src/main/java/tree/FindBottomLeftValue.java
Normal file
@ -0,0 +1,40 @@
|
||||
package tree;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* 题目: 513. 找树左下角的值 (FindBottomLeftValue )
|
||||
* 描述:给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。
|
||||
* 假设二叉树中至少有一个节点。
|
||||
*
|
||||
* 示例 1:
|
||||
输入: root = [2,1,3]
|
||||
输出: 1
|
||||
|
||||
* 链接:https://leetcode.cn/problems/find-bottom-left-tree-value/
|
||||
*/
|
||||
public class FindBottomLeftValue {
|
||||
public int findBottomLeftValue(TreeNode root) {
|
||||
int res=Integer.MIN_VALUE;
|
||||
Queue<TreeNode>queue=new LinkedList<>();
|
||||
queue.offer(root);
|
||||
while (!queue.isEmpty()){
|
||||
int sz=queue.size();
|
||||
boolean flag=false;
|
||||
for (int i = 0; i < sz; i++) {
|
||||
TreeNode cur=queue.poll();
|
||||
if(i==0)res=cur.val;
|
||||
if(cur.left!=null) {
|
||||
queue.add(cur.left);
|
||||
flag=true;
|
||||
}
|
||||
if(cur.right!=null){
|
||||
queue.add(cur.right);
|
||||
flag=true;
|
||||
}
|
||||
}
|
||||
if(!flag)return res;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
50
src/main/java/tree/SumOfLeftLeaves.java
Normal file
50
src/main/java/tree/SumOfLeftLeaves.java
Normal file
@ -0,0 +1,50 @@
|
||||
package tree;
|
||||
/**
|
||||
* 题目: 404. 左叶子之和 ( sumOfLeftLeaves)
|
||||
* 描述:给定二叉树的根节点 root ,返回所有左叶子之和。
|
||||
*
|
||||
* 示例 1:
|
||||
输入: root = [3,9,20,null,null,15,7]
|
||||
输出: 24
|
||||
解释: 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
|
||||
|
||||
* 链接:https://leetcode.cn/problems/sum-of-left-leaves/
|
||||
*/
|
||||
public class SumOfLeftLeaves {
|
||||
private int total = 0;
|
||||
|
||||
public int sumOfLeftLeaves(TreeNode root) {
|
||||
total = 0;
|
||||
dfs(root);
|
||||
return total;
|
||||
}
|
||||
|
||||
private void dfs(TreeNode node) {
|
||||
if (node == null) return;
|
||||
// 如果 node 的左孩子是叶子,累加
|
||||
if (node.left != null
|
||||
&& node.left.left == null
|
||||
&& node.left.right == null) {
|
||||
total += node.left.val;
|
||||
}
|
||||
// 继续遍历左右子树
|
||||
dfs(node.left);
|
||||
dfs(node.right);
|
||||
}
|
||||
public int sumOfLeftLeaves2(TreeNode root) {
|
||||
if (root == null) return 0;
|
||||
int sum = 0;
|
||||
// 如果左子节点是叶子,直接加上它的值
|
||||
if (root.left != null
|
||||
&& root.left.left == null
|
||||
&& root.left.right == null) {
|
||||
sum += root.left.val;
|
||||
} else {
|
||||
// 否则,递归累加左子树里的左叶子
|
||||
sum += sumOfLeftLeaves(root.left);
|
||||
}
|
||||
// 右子树也要继续找它的“左叶子”
|
||||
sum += sumOfLeftLeaves(root.right);
|
||||
return sum;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user