/**
* 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 {
private int deep = -1;
private int value = 0;
public int findBottomLeftValue(TreeNode root) {
value = root.val;
prevOrder(root, 0);
return value;
}
private void prevOrder(TreeNode root, int depth) {
if (root == null) return;
if (root.left == null && root.right == null) {
//depth记录遍历的深度 deep记录当前记录最左值结点深度
if (depth > deep) {
deep = depth;
value = root.val;
}
}
if (root.left != null) prevOrder(root.left, depth + 1);
if (root.right != null) prevOrder(root.right, depth + 1);
}
}
//层序迭代
// class Solution {
// public int findBottomLeftValue(TreeNode root) {
// Deque<TreeNode> queue = new LinkedList<>();
// queue.offer(root);
// int res = root.val;
// while (!queue.isEmpty()) {
// int size = queue.size();
// //每层第一个节点就是该层最左节点
// TreeNode cur = queue.poll();
// res = cur.val;
// //第一个节点子节点入队
// if (cur.left != null) queue.offer(cur.left);
// if (cur.right != null) queue.offer(cur.right);
// for (int i = 1; i < size; i++){
// //该层其他节点子节点入队
// cur = queue.poll();
// if (cur.left != null) queue.offer(cur.left);
// if (cur.right != null) queue.offer(cur.right);
// }
// }
// return res;
// }
// }
![]()
/**
* 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 {
public boolean hasPathSum(TreeNode root, int targetSum) {
//先放到该结点的路径和1,在放该结点
Deque<Object> stack = new LinkedList<>();
if (root != null){
stack.push(root.val);
stack.push(root);
}
while (!stack.isEmpty()) {
//中 取结点 取到该结点路径和
TreeNode cur = (TreeNode) stack.pop();
int sum = (int) stack.pop();
if (cur.left == null && cur.right == null && targetSum == sum) return true;
//右左 出栈处理时为左右
if (cur.right != null) {
stack.push(sum + cur.right.val);
stack.push(cur.right);
}
if (cur.left != null) {
stack.push(sum + cur.left.val);
stack.push(cur.left);
}
}
return false;
}
}
//递归简洁版
// class Solution {
// public boolean hasPathSum(TreeNode root, int targetSum) {
// //为空返回false
// if (root == null) return false;
// //到达叶子结点
// if (root.left == null && root.right == null) return targetSum == root.val;
// //寻找路径
// return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right,targetSum - root.val);
// }
// }
//递归前序
// class Solution {
// public boolean hasPathSum(TreeNode root, int targetSum) {
// if (root == null) return false;
// return prevOrder(root, targetSum - root.val);
// }
// private boolean prevOrder(TreeNode root, int count) {
// //中
// if (root.left == null && root.right == null && count == 0) return true;
// //遇到叶子结点且不符合要求直接返回false说明该条路径不合适
// if (root.left == null && root.right == null) return false;
// //左右 继续在左右子树寻找路径
// if (root.left != null)
// if (prevOrder(root.left, count - root.left.val)) return true;
// if (root.right != null)
// if (prevOrder(root.right, count - root.right.val)) return true;
// //未找到 返回false
// return false;
// }
// }
//递归前序 使用List存所有路径
// class Solution {
// private List<Integer> res;
// public boolean hasPathSum(TreeNode root, int targetSum) {
// if (root == null) return false;
// res = new ArrayList<>();
// prevOrder(root, root.val);
// for (int i : res) {
// if (i == targetSum) return true;
// }
// return false;
// }
// private void prevOrder(TreeNode root, int sum) {
// if (root.left == null && root.right == null) res.add(sum);
// else {
// if (root.left != null) prevOrder(root.left, sum + root.left.val);
// if (root.right != null) prevOrder(root.right, sum + root.right.val);
// }
// }
// }
![]()
//前序递归
class Solution {
private List<List<Integer>> res;
private List<Integer> path;
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
res = new ArrayList<>();
if (root == null) return res;
path = new LinkedList<>();
path.add(root.val);
prevOrder(root, targetSum - root.val);
return res;
}
private void prevOrder(TreeNode root, int count) {
if (root.left == null && root.right == null && count == 0) res.add(new ArrayList<>(path));
if (root.left == null && root.right == null) return;
if (root.left != null) {
path.add(root.left.val);
prevOrder(root.left, count - root.left.val);
path.remove(path.size() - 1);
}
if (root.right != null) {
path.add(root.right.val);
prevOrder(root.right, count - root.right.val);
path.remove(path.size() - 1);
}
}
}
![]()
//用下标划分界限 左闭右开
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
return buildTree(inorder, 0, inorder.length, postorder, 0, postorder.length);
}
private TreeNode buildTree(int[] inorder, int ls, int le, int[] postorder, int rs, int re) {
//用前序遍历结果集判断该树是否还有结点或还有一个结点的情况
//没有节点了
if (le - ls < 1) return null;
//只有一个结点
if (le - ls == 1) return new TreeNode(inorder[ls]);
//构造子树
//后序遍历最后一个结点为根节点
int rVal = postorder[re - 1];
TreeNode root = new TreeNode(rVal);
//定义变量存储根结点在前序遍历结果中的下标
int rootIndex = 0;
//在前序遍历结果中找到根节点做为界限
for (int i = ls; i < le; i++) {
if (inorder[i] == rVal) {
rootIndex = i;
break;
}
}
//构造子树的左子树和右子树 注意区间左闭右开
root.left = buildTree(inorder, ls, rootIndex, postorder, rs, rs + (rootIndex - ls));
root.right = buildTree(inorder, rootIndex + 1, le, postorder, rs + (rootIndex - ls), re - 1);
return root;
}
}
![]()
参考:https://programmercarl.com/0106.%E4%BB%8E%E4%B8%AD%E5%BA%8F%E4%B8%8E%E5%90%8E%E5%BA%8F%E9%81%8D%E5%8E%86%E5%BA%8F%E5%88%97%E6%9E%84%E9%80%A0%E4%BA%8C%E5%8F%89%E6%A0%91.html#java