即二叉树最底下一层中最左侧的节点
(1)遍历法
class Solution {
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> que = new LinkedList<TreeNode>();
que.offer(root);
int res = 0;
while (!que.isEmpty()) {
int len = que.size();
for(int i=0;i<len;i++){
TreeNode poll = que.poll();
if (i == 0) {
res = poll.val;
}
if (poll.left != null) {
que.offer(poll.left);
}
if (poll.right != null) {
que.offer(poll.right);
}
}
}
return res;
}
}
(2)递归法:使用到了回溯
定义深度deep和最终的值为class内部变量
*/
class Solution {
private int Deep = -1;
private int val = 0;
public int findBottomLeftValue(TreeNode root) {
val = root.val;
findLeftValue(root,Deep);
return val;
}
private void findLeftValue(TreeNode root,int deep) {
if (root == null) return;
if (root.left == null && root.right == null) {
if (deep > Deep) {
val = root.val;
Deep = deep;
}
}
if (root.left != null) findLeftValue(root.left,deep + 1);
if (root.right != null) findLeftValue(root.right,deep + 1);
}
}
其中
if (root.left != null) findLeftValue(root.left,deep + 1);
等价于 if (root.left != null){
deep++;
findLeftValue(root.left,deep + 1);
deep--;
}
2.路径总和
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;
}
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;
}
}
3.路径总和2
需要输出路径
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<>();
preorderdfs(root, targetSum, res, path);
return res;
}
private void preorderdfs(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path){
path.add(root.val);
if(root.left==null && root.right==null){
if(targetSum - root.val == 0){ //find leaf
res.add(new ArrayList<>(path));
}
return;
}
if(root.left!=null){
preorderdfs(root.left, targetSum - root.val, res, path);
path.remove(path.size() - 1);
}
if(root.right!=null){
preorderdfs(root.right, targetSum - root.val, res, path);
path.remove(path.size() - 1);
}
}
}
4.从中序与后序遍历序列构造二叉树
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;
}
}
5.从前序与中序遍历序列构造二叉树
class Solution {
Map<Integer, Integer> map;
public TreeNode buildTree(int[] preorder, int[] inorder) {
map = new HashMap<>();
for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置
map.put(inorder[i], i);
}
return findNode(preorder, 0, preorder.length, inorder, 0, inorder.length); // 前闭后开
}
public TreeNode findNode(int[] preorder, int preBegin, int preEnd, int[] inorder, int inBegin, int inEnd) {
// 参数里的范围都是前闭后开
if (preBegin >= preEnd || inBegin >= inEnd) { // 不满足左闭右开,说明没有元素,返回空树
return null;
}
int rootIndex = map.get(preorder[preBegin]); // 找到前序遍历的第一个元素在中序遍历中的位置
TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点
int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定前序数列的个数
root.left = findNode(preorder, preBegin + 1, preBegin + lenOfLeft + 1,
inorder, inBegin, rootIndex);
root.right = findNode(preorder, preBegin + lenOfLeft + 1, preEnd,
inorder, rootIndex + 1, inEnd);
return root;
}
}