目录
题目一:找树左下角的值
题目描述:给定一个二叉树的 根节点 root
,请找出该二叉树的 最底层 最左边 节点的值。
思路分析:(详细思路)
找到深度最大的叶子节点,使用前序遍历,优先保证从左边搜索
解法一:递归
class Solution {
private int maxDepth = -1; // 记录最大深度,初始化为最小值,方便进行比较
private int res = 0; // 深度最大值更新后,同时更新节点的值,存入res
public int findBottomLeftValue(TreeNode root) {
res = root.val;
findLeftValue(root, 0);
return res;
}
private void findLeftValue(TreeNode root, int depth) { // depth记录当前深度
if (root == null) {return;}
if (root.left == null && root.right == null) { // 找到叶子节点
if (depth > maxDepth) {
res = root.val;
maxDepth = depth;
}
}
if (root.left != null) findLeftValue(root.left, depth + 1); // 往下遍历时,深度加一
if (root.right != null) findLeftValue(root.right, depth + 1);
}
}
解法二:迭代
class Solution {
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int res = 0;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (i == 0) res = node.val;
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
}
return res;
}
}
题目二:路径总和
题目描述:
给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。
思路分析:(视频指路)
递归过程中找到一条符合的路径即可返回true
解法一:递归
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) return false;
targetSum -= root.val;
if (root.left == null && root.right == null) { // 找到叶子节点
return targetSum == 0; // 若为符合的路径返回true,否则为false
}
if (root.left != null) {
boolean left = hasPathSum(root.left, targetSum);
if (left) { // 根节点的左孩子方向有符合的路径
return true;
}
}
if (root.right != null) {
boolean right = hasPathSum(root.right, targetSum);
if (right) { // 根节点的右孩子方向有符合的路径
return true;
}
}
return false;
}
}
解法二:迭代
代码参考:代码随想录
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) return false;
Stack<TreeNode> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
stack1.push(root);
stack2.push(root.val);
while (!stack1.isEmpty()) {
TreeNode node = stack1.pop();
int sum = stack2.pop();
// 如果该节点是叶子节点了,同时该节点的路径数值等于sum,那么就返回true
if(node.left == null && node.right == null && sum == targetSum) return true;
// 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来
if(node.right != null){
stack1.push(node.right);
stack2.push(sum+node.right.val);
}
// 左节点,压进去一个节点的时候,将该节点的路径数值也记录下来
if(node.left != null){
stack1.push(node.left);
stack2.push(sum+node.left.val);
}
}
return false;
}
}
题二拓展:路径总和||
题目描述:给你二叉树的根节点 root
和一个整数目标和 targetSum
,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
代码参考:代码随想录
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res; // 非空判断
List<Integer> path = new LinkedList<>();
preorder(root, targetSum, res, path);
return res;
}
public void preorder(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path) {
path.add(root.val);
// 遇到叶子节点
if (root.left == null && root.right == null) {
// 找到了和为 targetSum 的路径
if (targetSum - root.val == 0) {
res.add(new ArrayList<>(path));
}
return;
}
if (root.left != null) {
preorder(root.left, targetSum - root.val, res, path);
path.remove(path.size() - 1); // 回溯
}
if (root.right != null) {
preorder(root.right, targetSum - root.val, res, path);
path.remove(path.size() - 1); // 回溯
}
}
}
题目三:从中序与后序遍历序列构造二叉树
题目描述:给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树
思路分析:本题涉及的细节很多,建议直接看讲解,视频指路
代码参考:代码随想录
class Solution {
Map<Integer, Integer> map; // 方便根据数值查找位置
public TreeNode buildTree(int[] inorder, int[] postorder) {
map = new HashMap<>();
for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置
map.put(inorder[i], i);
}
return findNode(inorder, 0, inorder.length, postorder,0, postorder.length); // 前闭后开
}
public TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) {
// 参数里的范围都是前闭后开
if (inBegin >= inEnd || postBegin >= postEnd) { // 不满足左闭右开,说明没有元素,返回空树
return null;
}
int rootIndex = map.get(postorder[postEnd - 1]); // 找到后序遍历的最后一个元素在中序遍历中的位置
TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点
int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定后序数列的个数
root.left = findNode(inorder, inBegin, rootIndex, postorder, postBegin, postBegin + lenOfLeft);
root.right = findNode(inorder, rootIndex + 1, inEnd, postorder, postBegin + lenOfLeft, postEnd - 1);
return root;
}
}
版权声明:本文为weixin_45977348原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。