学习目标
- 完成二叉树(回溯)相关题目
学习内容
513. 找树左下角的值(Middle)
题目链接:513. 找树左下角的值
题目:给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点。
示例 1:
输入: root = [2,1,3]
输出: 1
示例 2:
输入: [1,2,3,4,null,5,6,null,null,7]
输出: 7
思路:递归法一开始还没思路,想到的是迭代法套用模板.
递归时间复杂度:O(n)
空间复杂度:O(log n)
迭代时间福州度:O(n)
空间复杂度:O(n)
解决方案:
递归:
class Solution {
private int maxDepth = Integer.MIN_VALUE;
private int result = 0;
public int findBottomLeftValue(TreeNode root) {
find(root, 0);
return result;
}
private void find(TreeNode root, int depth) {
if (root.left == null && root.right == null) {
if(maxDepth < depth) {
maxDepth = depth;
result = root.val;
}
return;
}
if(root.left != null) {
depth ++;
find(root.left, depth);
depth --;
}
if(root.right != null) {
depth ++;
find(root.right, depth);
depth --;
}
}
}
迭代:
class Solution {
public int findBottomLeftValue(TreeNode root) {
int result = 0;
Deque<TreeNode> deque = new ArrayDeque<>();
deque.offer(root);
while(!deque.isEmpty()) {
int len = deque.size();
boolean flag = true;
while(len > 0) {
TreeNode tmp = deque.poll();
if(tmp.left != null) deque.offer(tmp.left);
if(tmp.right != null) deque.offer(tmp.right);
if(flag) {
result = tmp.val;
flag = false;
}
len --;
}
}
return result;
}
}
小结:虽然时间复杂都是 O(n), 但是递归法会快一些.
112. 路径总和(Easy)
题目链接:112. 路径总和
题目:给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。
叶子节点 是指没有子节点的节点。
示例 1:
输入:root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
输出:true
解释:等于目标和的根节点到叶节点路径如上图所示。
示例 2:
输入:root = [1,2,3], targetSum = 5
输出:false
解释:树中存在两条根节点到叶子节点的路径:
(1 --> 2): 和为 3
(1 --> 3): 和为 4
不存在 sum = 5 的根节点到叶子节点的路径。
示例 3:
输入:root = [], targetSum = 0
输出:false
解释:由于树是空的,所以不存在根节点到叶子节点的路径。
思路:递归 + 回溯
时间复杂度:O(n)
空间复杂度:O(log n)
(平均情况, 若是链状则O(N))
解决方案:
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null) return false;
if (root.left == null && root.right == null) {
return root.val == targetSum;
}
boolean left = false, right = false;
if(root.left != null) {
left = hasPathSum(root.left, targetSum - root.val);
}
if(root.right != null) {
right = hasPathSum(root.right, targetSum - root.val);
}
return left || right;
}
}
小结:这里其实是比较隐式的回溯了, 比如 left = hasPathSum(root.left, targetSum - root.val);
相当于
targetSum -= root.val;
left = hasPathSum(root.left, targetSum);
targetSum += root.val;
113. 路径总和 II(Middle)
题目链接:113. 路径总和 II
题目:给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
叶子节点 是指没有子节点的节点。
示例 1:
输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
输出:[[5,4,11,2],[5,8,4,5]]
示例 2:
输入:root = [1,2,3], targetSum = 5
输出:[]
示例 3:
输入:root = [1,2], targetSum = 0
输出:[]
思路:递归 + 回溯, 回溯的代码风格很多, 主要还是要自己理解.
时间复杂度:O(n^2)
? 官方给的题解是O(n^2)
, 自认为是O(n)
空间复杂度:O(log n)
(不计算 ans 的存放空间的话, 若计算则应当是 O(n) 以上)
解决方案:
class Solution {
List<List<Integer>> ans = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
preOrder(root, targetSum, new LinkedList<>());
return ans;
}
private void preOrder(TreeNode root, int targetSum, List<Integer> path) {
if(root == null) return;
if(root.left == null && root.right == null && root.val == targetSum) {
path.add(root.val);
ans.add(new ArrayList(path));
path.remove(path.size() - 1);
}
path.add(root.val);
if(root.left != null) {
preOrder(root.left, targetSum - root.val, path);
}
if(root.right != null) {
preOrder(root.right, targetSum - root.val, path);
}
path.remove(path.size() - 1);
}
}
小结:
106.从中序与后序遍历序列构造二叉树(Middle)
题目链接:106.从中序与后序遍历序列构造二叉树
题目:给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。
思路:
上面图片可以看到, 在中序中, 左子树数组(设长度为L), 正好就是后序中前L个元素组成的数组.
那么我们可以有以下思路:
rootIndex =
从后序数组中选中最后一个,根据这个数在中序数组中找到 root 的位置
root.left =
从 中序数组[中序数组开始位置, rootIndex)
和 后序数组[后序开始位置, rootIndex - 中序数组开始位置)中选
root.right =
从 中序数组[rootIndex + 1, 中序数组结束位置)
和 后序数组[后序开始位置 + (rootIndex - 中序数组开始位置), 后序数组结束位置 - 1)中选 (-1是去掉root)
后面则是引入递归, 确定边界, 以及在中序数组中找到 root 的位置
提取关键字数组中找到, 使用Map. 这类题要多写.
时间复杂度:O(n)
空间复杂度:O(n)
解决方案:
class Solution {
private Map<Integer, Integer> map = new HashMap<>();
public TreeNode buildTree(int[] inorder, int[] postorder) {
for(int i = 0; i < inorder.length; i++) {
map.put(inorder[i], i);
}
return build(inorder, 0, inorder.length, postorder, 0, postorder.length);
}
private TreeNode build(int[] inorder, int inStart, int inEnd,
int[] postorder, int postStart, int postEnd) {
if(inStart >= inEnd && postStart >= postEnd) {
return null;
}
int rootIndex = map.get(postorder[postEnd - 1]);
int leftTreeLen = rootIndex - inStart;
TreeNode root = new TreeNode(inorder[rootIndex]);
root.left = build(inorder, inStart, rootIndex,
postorder, postStart, postStart + leftTreeLen);
root.right = build(inorder, rootIndex + 1, inEnd,
postorder, postStart + leftTreeLen, postEnd - 1);
return root;
}
}
小结:
105.从前序与中序遍历序列构造二叉树(Middle)
题目链接:105.从前序与中序遍历序列构造二叉树
题目:给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。
示例 1:
输入: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
输出: [3,9,20,null,null,15,7]
示例 2:
输入: preorder = [-1], inorder = [-1]
输出: [-1]
思路:同上
时间复杂度:O(n)
空间复杂度:O(n)
解决方案:
class Solution {
private Map<Integer, Integer> map = new HashMap<>();
public TreeNode buildTree(int[] preorder, int[] inorder) {
for(int i = 0; i < inorder.length; i++) {
map.put(inorder[i], i);
}
return build(preorder, 0, preorder.length, inorder, 0, inorder.length);
}
private TreeNode build(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd) {
if(preStart >= preEnd || inStart >= inEnd) {
return null;
}
int rootIndex = map.get(preorder[preStart]);
int leftTreeLen = rootIndex - inStart;
TreeNode root = new TreeNode(inorder[rootIndex]);
root.left = build(preorder, preStart + 1, preStart + leftTreeLen + 1, inorder, inStart, inStart + leftTreeLen);
root.right = build(preorder, preStart + leftTreeLen + 1, preEnd, inorder, inStart + leftTreeLen + 1, inEnd);
return root;
}
}
小结:
上面的 root.left = build(preorder, preStart + 1, preStart + leftTreeLen + 1, inorder, inStart, inStart + leftTreeLen);
中, 显然 inStart + leftTreeLen 可以换成 rootIndex.
总结
今天的题目感觉难道骤升, 应该是不熟悉题目的缘故, 建议明天复习.