这是我参与8月更文挑战的第16天,活动详情查看:8月更文挑战
剑指 Offer 33. 二叉搜索树的后序遍历序列
题目
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true,否则返回 false。假设输入的数组的任意两个数字都互不相同。
参考以下这颗二叉搜索树:
5
/ \
2 6
/ \
1 3
示例 1:
输入: [1,6,3,2,5]
输出: false
示例 2:
输入: [1,3,2,6,5]
输出: true
提示:
数组长度 <= 1000
方法一
单调栈:后序遍历的顺序为左->右->根,我们可以将给定的后序遍历数组翻转,则遍历的顺序变为了根->右->左,这样就和先序遍历类似。整个数组分成若干段,局部看,根据根->右->左的性质,就会有先变大后变小的规律。利用这个规律,我们可以使用单调栈来求解,当当前遍历到的元素大于栈顶元素时,说明当前该元素在某颗子树的右子树中,压入栈中;当遇到当前结点小于栈顶元素时,说明某颗子树的右子树已经全部遍历完,而且当前结点在其左子树中,将栈中大于该结点的值全部弹出,最后一个弹出的大于该结点的值即为该结点的父结点,更新root的值,再往后遍历过程中若有大于root的结点,说明非法,即(左子树中有大于根结点的值);全部遍历完后,若无非法,则说明是二叉搜索树的后序遍历,返回true
class Solution {
public boolean verifyPostorder(int[] postorder) {
int n = postorder.length;
LinkedList<Integer> stk = new LinkedList<>();
int root = Integer.MAX_VALUE;
for (int i = n - 1; i >= 0; i -- ) {
int cur = postorder[i];
if (cur > root) return false;
while(stk.size() > 0 && stk.getLast() > cur) {
root = stk.getLast();
stk.removeLast();
}
stk.addLast(cur);
}
return true;
}
}
时间复杂度: O(n)
空间复杂度: O(n)
方法二
递归:题中给出后序遍历,若为二叉搜索树,则中序遍历是一个升序数组,知道了中序和后序遍历,可以构建出一个二叉树。用中序和后序遍历递归去创建二叉树,若创建过程中,发现无法构建则返回false,构建成功返回true
class Solution {
HashMap<Integer, Integer> map = new HashMap<>();
public boolean verifyPostorder(int[] postorder) {
int n = postorder.length;
int[] midorder = (int[])Arrays.copyOf(postorder, postorder.length);
Arrays.sort(midorder);
for (int i = 0; i < n; i ++ )
map.put(midorder[i], i);
return dfs(0, n - 1, 0, n - 1, midorder, postorder);
}
public boolean dfs(int ml, int mr, int pl, int pr, int[] midorder, int[] postorder) {
if (pl > pr) return true;
int index = map.get(postorder[pr]);
//判断能否构建
HashMap<Integer, Integer> tmp = new HashMap<>();
int i = ml, j = pl;
for (; i < index; i ++, j ++ ) {
tmp.put(midorder[i], tmp.getOrDefault(midorder[i], 0) + 1);
tmp.put(postorder[j], tmp.getOrDefault(postorder[j], 0) + 1);
}
for (int x : tmp.keySet()) {
if (tmp.get(x) != 2) //不等于2说明中序或后序中只有一方有这个数,无法构建
return false;
}
return dfs(ml, i - 1, pl, j - 1, midorder, postorder) &&
dfs(i + 1, mr, j, pr - 1, midorder, postorder);
}
}
剑指 Offer 34. 二叉树中和为某一值的路径
题目
输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径。从树的根节点开始往下一直到叶节点所经过的节点形成一条路径。
示例: 给定如下二叉树,以及目标和 target = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
返回:
[
[5,4,11,2],
[5,8,4,5]
]
提示:
节点总数 <= 10000
方法一
回溯:从根结点递归向下,减去当前结点的值,若target=0说明已经满足,再另外判断是否为叶子结点即可;若target!=0继续向下递归;当回到这一层时,该层结束,恢复现场,将该结点从path中删除;
/**
* 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 {
List<List<Integer>> res = new LinkedList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> pathSum(TreeNode root, int target) {
if (root == null) return res;
dfs(root, target);
return res;
}
public void dfs(TreeNode root, int target) {
if (root == null) return;
path.add(root.val);
target -= root.val;
if (target == 0 && root.left == null && root.right == null)
res.add(new LinkedList(path));
dfs(root.right, target);
dfs(root.left, target);
path.removeLast();
}
}
时间复杂度: O(n)
空间复杂度: O(n)