二叉树 5
找树左下角的值
给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点。
思路
本题利用队列层序打印较简单,保留每一层最先出队的值,即可
递归思路较难,需要考虑的是最底层最左侧的值,因此要找到最底层,才记录值,
迭代
class Solution {
public int findBottomLeftValue(TreeNode root) {
Deque<TreeNode> queue = new ArrayDeque<>();
queue.offer(root);
int left = 0;
while(!queue.isEmpty()){
int size = queue.size();
for(int i = 0;i < size;i++){
TreeNode tmp = queue.poll();
if(i == 0)
left = tmp.val;
if(tmp.left != null)
queue.offer(tmp.left);
if(tmp.right != null)
queue.offer(tmp.right);
}
}
return left;
}
}
递归
class Solution {
int maxDep = Integer.MIN_VALUE;
int res = 0;
public int findBottomLeftValue(TreeNode root) {
recur(root,0);
return res;
}
public void recur(TreeNode root,int dep){
if(root.left == null && root.right == null){
if(dep > maxDep){
maxDep = dep;
res = root.val;
}
return;
}
if(root.left != null)
recur(root.left,dep + 1);
if(root.right != null)
recur(root.right,dep + 1);
}
}
路径总和 1
给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。
叶子节点 是指没有子节点的节点。
路径总和 2
给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
叶子节点 是指没有子节点的节点。
思路
两个思路一样的题目,需要注意的是到叶子节点,以及回溯操作,移除元素。
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;
}
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> tmp = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
if(root == null)
return res;
recur(root,targetSum);
return res;
}
public void recur(TreeNode root,int tar){
if(root.left == null && root.right == null){
if(root.val == tar){
tmp.add(root.val);
res.add(new ArrayList<>(tmp));
tmp.remove(tmp.size() - 1);
}
return;
}
if(root.left != null){
tmp.add(root.val);
recur(root.left,tar - root.val);
tmp.remove(tmp.size() - 1);
}
if(root.right != null){
tmp.add(root.val);
recur(root.right,tar - root.val);
tmp.remove(tmp.size() - 1);
}
}
}
从中序和后序遍历序列中构造二叉树
给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
思路
通过中序遍历和后序遍历的数组特征,进行递归构造左右子树
在构造的过程中注意边界细节
class Solution {
// 从中序和后序中找出对应关系
// 左 根 右 左 右 根
// 存储中序遍历中根的下标,便于查找
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);
}
// 注意参数,长度不 - 1
TreeNode root = helper(inorder,0,inorder.length ,postorder,0,postorder.length);
return root;
}
public TreeNode helper(int[] inorder,int inBegin,int inEnd,int[] postorder,int postBegin,int postEnd){
if(inEnd <= inBegin || postEnd <= postBegin){
return null;
}
int rootInd = map.get(postorder[postEnd - 1]);
TreeNode root = new TreeNode(inorder[rootInd]);
int len = rootInd - inBegin;
// 确定接下来递归边界
root.left = helper(inorder,inBegin,rootInd,postorder,postBegin,postBegin + len);
root.right = helper(inorder,rootInd + 1,inEnd,postorder,postBegin + len,postEnd - 1);
return root;
}
}