这道题目 使用层序遍历比较方便简单 也比较好想到
class Solution {
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
int res = 0;
queue.offer(root);
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;
}
}
递归的方法 可以使用回溯
class Solution {
int depth = -1;
int Deep = 0;
int res = 0;
public int findBottomLeftValue(TreeNode root) {
findValue(root);
return res;
}
private void findValue(TreeNode root){
if(root == null){
return;
}
if (root.left == null && root.right == null) {
if (Deep > depth) {
res = root.val;
depth = Deep;
}
}
if(root.left != null){
Deep++;
findValue(root.left);
Deep--;
}
if(root.right != null){
Deep++;
findValue(root.right);
Deep--;
}
}
}
回溯题
一般回溯都不需要返回值
但是对于这道题,找到了一条符合要求的路径就可以返回,不需要在递归下去了,所以回溯函数需要返回值来提前结束递归
- 如果要搜索其中一条符合条件的路径,那么递归一定需要返回值,因为遇到符合条件的路径了就要及时返回。(本题的情况)
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null){
return false;
}
return backtracking(root, root.val, targetSum);
}
private boolean backtracking(TreeNode root, int sum, int targetSum){
if(root.left == null && root.right == null){
if(sum == targetSum){
return true;
}else{
return false;
}
}
if(root.left != null){
sum += root.left.val;
if(backtracking(root.left, sum, targetSum)){
return true;
}
sum -= root.left.val;
}
if(root.right != null){
sum += root.right.val;
if(backtracking(root.right, sum, targetSum)){
return true;
}
sum -= root.right.val;
}
return false;
}
}
回溯法
是另外一种情况 需要找到所有符合要求的解
所以需要遍历全部的节点 不需要提前返回
所以回溯函数不需要返回值
- 如果需要搜索整棵二叉树且不用处理递归返回值,递归函数就不要返回值。(这种情况就是本文下半部分介绍的113.路径总和ii)
class Solution {
List<Integer> path = new ArrayList<>();
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
if(root == null){
return res;
}
path.add(root.val);
backtracking(root, root.val, targetSum, path);
return res;
}
private void backtracking(TreeNode root, int sum, int targetSum, List<Integer> path){
if(root.left == null && root.right == null){
if(sum == targetSum){
res.add(new ArrayList<>(path));
}
return;
}
if(root.left != null){
sum += root.left.val;
path.add(root.left.val);
backtracking(root.left, sum, targetSum, path);
sum -= root.left.val;
path.remove(path.size() - 1);
}
if(root.right != null){
sum += root.right.val;
path.add(root.right.val);
backtracking(root.right, sum, targetSum, path);
sum -= root.right.val;
path.remove(path.size() - 1);
}
}
}
对于回溯函数需不需要返回值
总结
再来看返回值,递归函数什么时候需要返回值?什么时候不需要返回值?这里总结如下三点:
- 如果需要搜索整棵二叉树且不用处理递归返回值,递归函数就不要返回值。(这种情况就是本文下半部分介绍的113.路径总和ii)
- 如果需要搜索整棵二叉树且需要处理递归返回值,递归函数就需要返回值。 (这种情况我们在236. 二叉树的最近公共祖先 (opens new window)中介绍)
- 如果要搜索其中一条符合条件的路径,那么递归一定需要返回值,因为遇到符合条件的路径了就要及时返回。(本题的情况)
按照转换的思路
主要是要确定边界 遵循左闭右开
class Solution {
Map<Integer, Integer> map = null;
public TreeNode buildTree(int[] inorder, int[] postorder) {
map = new HashMap<>();
for(int i = 0; i < inorder.length; i++){
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;
}
}