513.找树左下角的值
题目:513. 找树左下角的值 - 力扣(LeetCode)
题解:代码随想录 (programmercarl.com)
层序很好想,每次遍历记录最左边节点
递归。。。
solution
层序
public class Solution {
public int FindBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new Queue<TreeNode>();
int leftval = 0;
queue.Enqueue(root);
TreeNode temp = new TreeNode();
while(queue.Count > 0)
{
int n = queue.Count;
for(int i = 0; i < n; i ++)
{
temp = queue.Dequeue();
if(i == 0) leftval = temp.val;
if(temp.left != null) queue.Enqueue(temp.left);
if(temp.right != null) queue.Enqueue(temp.right);
}
}
return leftval;
}
}
递归
public class Solution {
public int maxDepth = -1;
public int leftmostValue;
public int FindBottomLeftValue(TreeNode root) {
FindBottomLeftValueHelper(root, 0);
return leftmostValue;
}
public void FindBottomLeftValueHelper(TreeNode node, int depth) {
if (node == null) return;
// 如果这是新一层的第一个节点
if (depth > maxDepth) {
maxDepth = depth;
leftmostValue = node.val;
}
// 先遍历左子树以确保找到最左边的值
FindBottomLeftValueHelper(node.left, depth + 1);
FindBottomLeftValueHelper(node.right, depth + 1);
}
}
summary
112.路经总和
题目:112. 路径总和 - 力扣(LeetCode)
题解:代码随想录 (programmercarl.com)
带返回值的递归总是想不利索。。。。逻辑稀烂,写的我生气!!!!!!!!!!!!!!!!
然后写了个拖沓的解法
solution
public class Solution {
public int sum = 0;
List<int> results = new List<int>();
public bool HasPathSum(TreeNode root, int targetSum) {
if(root == null) return false;
Traversal(root);
foreach(int i in results)
{
if(i == targetSum) return true;
}
return false;
}
public void Traversal(TreeNode root)
{
sum += root.val;
if(root.left == null && root.right == null)
{
results.Add(sum);
sum -= root.val;
return;
}
if(root.left != null) Traversal(root.left);
if(root.right != null) Traversal(root.right);
sum -= root.val;
}
}
summary
回头看错的离答案已经很近了,列一下错误代码,感觉还是没有把回溯理解透彻
public class Solution {
public bool HasPathSum(TreeNode root, int targetSum) {
if(root == null || targetSum < 0) return false;
if(root.left == null && root.right == null) return targetSum == 0;
targetSum -= root.val;
bool result = HasPathSum(root.left, targetSum) || HasPathSum(root.right, targetSum);
targetSum += root.val;
return result;
}
}
正确代码
public class Solution {
public bool HasPathSum(TreeNode root, int targetSum) {
if(root == null) return false;
targetSum -= root.val;
if(root.left == null && root.right == null) return targetSum == 0;
bool result = HasPathSum(root.left, targetSum) || HasPathSum(root.right, targetSum);
return result;
}
}
错误:
1、
113.路径总和Ⅱ
题目:113. 路径总和 II - 力扣(LeetCode)
题解:代码随想录 (programmercarl.com)
有了上一题的铺垫,这一题倒是比较容易写出来了。
需要总结的是,何种数据类型的何种形式怎样传递,列表作为参数和作为全局变量,整数作为参数和作为全局变量都是什么情形
solution
public class Solution {
List<IList<int>> results = new List<IList<int>>();
List<int> path = new List<int>();
public IList<IList<int>> PathSum(TreeNode root, int targetSum) {
Traversal(root, targetSum);
return results;
}
public void Traversal(TreeNode root, int targetSum)
{
if(root == null) return;
path.Add(root.val);
targetSum -= root.val;
if(root.left == null && root.right == null && targetSum == 0)
{
results.Add(new List<int>(path));
path.RemoveAt(path.Count - 1);
return;
}
Traversal(root.left, targetSum);
Traversal(root.right, targetSum);
path.RemoveAt(path.Count - 1);
}
}
summary
106.从中序和后序遍历构造二叉树
题目:106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)
题解:代码随想录 (programmercarl.com)
无它,背掉
solution
public class Solution {
public TreeNode BuildTree(int[] inorder, int[] postorder) {
// 边界情况:如果中序遍历数组或后序遍历数组为空,则返回 null
if (inorder.Length == 0 || postorder.Length == 0)
return null;
// 构建哈希表,存储中序遍历数组元素到索引的映射
Dictionary<int, int> indexMap = new Dictionary<int, int>();
for (int i = 0; i < inorder.Length; i++)
{
indexMap[inorder[i]] = i;
}
// 调用递归函数构建二叉树
return BuildTreeHelper(inorder, 0, inorder.Length - 1, postorder, 0, postorder.Length - 1, indexMap);
}
private TreeNode BuildTreeHelper(int[] inorder, int inStart, int inEnd,
int[] postorder, int postStart, int postEnd,
Dictionary<int, int> indexMap)
{
// 递归的基本情况
if (inStart > inEnd || postStart > postEnd)
return null;
// 后序遍历数组的最后一个元素是当前子树的根节点
int rootValue = postorder[postEnd];
TreeNode root = new TreeNode(rootValue);
// 在中序遍历数组中找到根节点的索引
int rootIndexInorder = indexMap[rootValue];
// 计算左子树和右子树的大小
int leftSubtreeSize = rootIndexInorder - inStart;
// 递归构建左右子树
root.left = BuildTreeHelper(inorder, inStart, rootIndexInorder - 1,
postorder, postStart, postStart + leftSubtreeSize - 1,
indexMap);
root.right = BuildTreeHelper(inorder, rootIndexInorder + 1, inEnd,
postorder, postStart + leftSubtreeSize, postEnd - 1,
indexMap);
return root;
}
}
summary
105.从前序与中序遍历构造二叉树
题目:105. 从前序与中序遍历序列构造二叉树 - 力扣(LeetCode)
题解:代码随想录 (programmercarl.com)
solution
public class Solution {
public TreeNode BuildTree(int[] preorder, int[] inorder) {
// 边界情况:如果前序遍历数组或中序遍历数组为空,则返回 null
if (preorder.Length == 0 || inorder.Length == 0)
return null;
// 构建哈希表,存储中序遍历数组元素到索引的映射
Dictionary<int, int> indexMap = new Dictionary<int, int>();
for (int i = 0; i < inorder.Length; i++)
{
indexMap[inorder[i]] = i;
}
// 调用递归函数构建二叉树
return BuildTreeHelper(preorder, 0, preorder.Length - 1, inorder, 0, inorder.Length - 1, indexMap);
}
private TreeNode BuildTreeHelper(int[] preorder, int preStart, int preEnd,
int[] inorder, int inStart, int inEnd,
Dictionary<int, int> indexMap)
{
// 递归的基本情况
if (preStart > preEnd || inStart > inEnd)
return null;
// 前序遍历数组的第一个元素是当前子树的根节点
int rootValue = preorder[preStart];
TreeNode root = new TreeNode(rootValue);
// 在中序遍历数组中找到根节点的索引
int rootIndexInorder = indexMap[rootValue];
// 计算左子树和右子树的大小
int leftSubtreeSize = rootIndexInorder - inStart;
// 递归构建左右子树
root.left = BuildTreeHelper(preorder, preStart + 1, preStart + leftSubtreeSize,
inorder, inStart, rootIndexInorder - 1,
indexMap);
root.right = BuildTreeHelper(preorder, preStart + leftSubtreeSize + 1, preEnd,
inorder, rootIndexInorder + 1, inEnd,
indexMap);
return root;
}
}