二叉树part4
二叉树part4
513. 找树左下角的值 - 力扣(LeetCode)
一眼层序遍历,最后一层的头val就是res
class Solution {
public int findBottomLeftValue(TreeNode root) {
int res = 0;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
int size = queue.size();
for(int i = 0; i < size; i++){
TreeNode cur = queue.poll();
if(i == 0){
res = cur.val;
}
if(cur.left != null){
queue.add(cur.left);
}
if(cur.right != null){
queue.add(cur.right);
}
}
}
return res;
}
}
112. 路径总和 - 力扣(LeetCode)
迭代比较好理解
class Solution {
Stack<TreeNode> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null){
return false;
}
stack1.push(root);
stack2.push(root.val);
while(!stack1.isEmpty()){
int size = stack1.size();
for(int i = 0; i < size; i++){
TreeNode cur = stack1.pop();
int sum = stack2.pop();
if(cur.left == null && cur.right == null && sum == targetSum){
return true;
}
if(cur.left != null){
stack1.push(cur.left);
stack2.push(sum + cur.left.val);
}
if(cur.right != null){
stack1.push(cur.right);
stack2.push(sum + cur.right.val);
}
}
}
return false;
}
}
113. 路径总和 II - 力扣(LeetCode)
这个题目重点在于写出dfs的函数,参数和返回值是最难确定的
class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
if(root == null){
return res;
}
List<Integer> path = new ArrayList<>();
dfs(root, res, path, targetSum);
return res;
}
public void dfs(TreeNode cur, List<List<Integer>> res, List<Integer> path, int targetSum){
path.add(cur.val);
if(cur.left == null && cur.right == null){ //叶子节点
if(targetSum - cur.val == 0){ //找到路径
res.add(new ArrayList<>(path));
}
return;
}
if(cur.left != null){
dfs(cur.left, res, path, targetSum - cur.val);
path.remove(path.size() - 1);
}
if(cur.right != null){
dfs(cur.right, res, path, targetSum - cur.val);
path.remove(path.size() - 1);
}
}
}
106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)
递归思路,分割数组
- 第一步:如果数组大小为零的话,说明是空节点了。
- 第二步:如果不为空,那么取后序数组最后一个元素作为节点元素。
- 第三步:找到后序数组最后一个元素在中序数组的位置,作为切割点
- 第四步:切割中序数组,切成中序左数组和中序右数组 (顺序别搞反了,一定是先切中序数组)
- 第五步:切割后序数组,切成后序左数组和后序右数组
- 第六步:递归处理左区间和右区间
需要注意几个点,Java中的数组切片操作使用Arrays.copyOfRange()函数,此函数为左闭右开类型
int[] test_int = new int[] { 1, 2, 3, 4, 5};
test_int = Arrays.copyOfRange(test_int, 1, 4);
System.out.println(Arrays.toString(test_int));
运行结果:
[2,3,4]
所以在切片数组的时候,要注意索引的使用,其实好想,之前二分法学明白了想的就很通畅
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder.length == 0){
return null;
}
int value = postorder[postorder.length - 1];
TreeNode root = new TreeNode(value);
if(inorder.length == 1){
return root;
}
int i = 0;
for(; i < inorder.length; i++){
if(inorder[i] == value){
break;
}
}
int[] inorderLeft = Arrays.copyOfRange(inorder, 0, i);
int[] inorderRight = Arrays.copyOfRange(inorder, i+1, inorder.length);
int[] postorderLeft = Arrays.copyOfRange(postorder, 0, i);
int[] postorderRight = Arrays.copyOfRange(postorder, i, inorder.length - 1);
root.left = buildTree(inorderLeft, postorderLeft);
root.right = buildTree(inorderRight, postorderRight);
return root;
}
}
浙公网安备 33010602011771号