513.找树左下角的值
给定一个二叉树,在树的最后一行找到最左边的值。
示例 1:
示例 2:
#算法公开课
《代码随想录》算法视频公开课 (opens new window):怎么找二叉树的左下角? 递归中又带回溯了,怎么办?| LeetCode:513.找二叉树左下角的值 (opens new window),相信结合视频在看本篇题解,更有助于大家对本题的理解。
#思路
本题要找出树的最后一行的最左边的值。此时大家应该想起用层序遍历是非常简单的了,反而用递归的话会比较难一点。
我们依然还是先介绍递归法。
解答: 我先用层序遍历写了一遍,这个比较慢一点,但是确实能比较准确的求出来.
class Solution {
public int findBottomLeftValue(TreeNode root) {
//用层序遍历来做一下试试
Queue<TreeNode> que = new LinkedList<>();
int result = 0;
que.offer(root);
while(que.size() != 0){
int len = que.size();
TreeNode tmpNode = que.poll();
result = tmpNode.val;
if(tmpNode.left != null )que.offer(tmpNode.left);
if(tmpNode.right != null )que.offer(tmpNode.right);
len--;
while(len != 0){
tmpNode = que.poll();
if(tmpNode.left != null )que.offer(tmpNode.left);
if(tmpNode.right != null )que.offer(tmpNode.right);
len--;
}
}
return result;
}
}
用递归来写, 注意这里的回溯已经隐藏在deepth+1中了.不需要再刻意对deepth - 1了.
class Solution {
//定义全局变量
int max_depth = -1;
int result = 0 ;
public int findBottomLeftValue(TreeNode root) {
//利用求最大深度,然后求最大深度的最左侧的值
result = root.val;
traverse(0 , root);
return result;
}
public void traverse(int deepth, TreeNode root){
//依然用递归,但是还要加上回溯
//前序遍历的方法
if(root == null)return;
if(root.left == null && root.right == null){
if(deepth > max_depth){
max_depth = deepth;
result = root.val;
}
}
if(root.left != null){//左,回溯
traverse(deepth + 1,root.left);//这里已经隐藏回溯了,没有直接加deepth 的值
}
if(root.right != null){//右,回溯
traverse(deepth + 1,root.right);
}
}
}
112. 路径总和
给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
说明: 叶子节点是指没有子节点的节点。
示例: 给定如下二叉树,以及目标和 sum = 22,
返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。
#算法公开课
《代码随想录》算法视频公开课 (opens new window):拿不准的遍历顺序,搞不清的回溯过程,我太难了! | LeetCode:112. 路径总和 (opens new window),相信结合视频在看本篇题解,更有助于大家对本题的理解。
题解: 这个题是自己写出来的,而且意外的和答案几乎一样, 说明现在的思路和思考逻辑基本是顺畅的了. 注意定义返回值的情况是什么, 把握解题思路.
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null) return false;
return traverse(root, 0,targetSum);
}
private boolean traverse(TreeNode root, int sum, int targetSum){
if(root.left == null && root.right == null) {
int all = sum + root.val;
if(all == targetSum) return true;
else return false;
}
if(root.left != null){
if(traverse(root.left, sum + root.val, targetSum)) return true;
}
if(root.right != null){
if(traverse(root.right, sum + root.val, targetSum)) return true;
}
return false;
}
}
看完本文,可以一起解决如下两道题目
- 106.从中序与后序遍历序列构造二叉树
- 105.从前序与中序遍历序列构造二叉树
#106.从中序与后序遍历序列构造二叉树
根据一棵树的中序遍历与后序遍历构造二叉树。
注意: 你可以假设树中没有重复的元素。
例如,给出
- 中序遍历 inorder = [9,3,15,20,7]
- 后序遍历 postorder = [9,15,7,20,3] 返回如下的二叉树:
#算法公开课
《代码随想录》算法视频公开课 (opens new window):坑很多!来看看你掉过几次坑 | LeetCode:106.从中序与后序遍历序列构造二叉树 (opens new window),相信结合视频在看本篇题解,更有助于大家对本题的理解。
题解: 这个题要是忘记了, 先回想二叉树遍历特征, 再思考数组的组成, 后序的最后一个必定是节点, 以这个为突破口进行递归.还有就是注意前闭后开这类规则的前后统一
class Solution {
Map<Integer, Integer> map;
public TreeNode buildTree(int[] inorder, int[] postorder) {
map = new HashMap<>();//用map 储存中序序列数值对应的位置;
for(int i = 0; i < inorder.length; i++){
map.put(inorder[i], i);
}
return findTree(inorder, 0 , inorder.length, postorder, 0, inorder.length );//前闭后开
}
private TreeNode findTree(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 = findTree(inorder, inbegin, rootIndex,
postorder,postbegin,postbegin + lenOfLeft);
root.right = findTree(inorder, rootIndex+1, inend,
postorder, postbegin + lenOfLeft, postend-1);
return root;
}
}