首页 > 其他分享 >day13打卡

day13打卡

时间:2024-08-30 21:26:32浏览次数:4  
标签:return int traversal depth day13 打卡 root postorder

树左下角值

class Solution
{
public:
void traversal(TreeNode root, int depth, int &ret, int &maxdepth)
{
if(root->left == nullptr && root->right == nullptr)
{
if(depth > maxdepth)
{
maxdepth = depth;
ret = root->val;
}
}
if(root->left)
{
depth++;
traversal(root->left, depth, ret, maxdepth);
--depth;
}
if(root->right)
{
++depth;
traversal(root->right, depth, ret, maxdepth);
--depth;
}
}
int findBottomLeftValue(TreeNode
root)
{
int depth = 1;
int ret = 0;
int maxdepth = INT_MIN;
traversal(root, depth, ret, maxdepth);
return ret;
}
};

路径总和

class Solution
{
public:
bool traversal(TreeNode root, int count)
{
if(root->left == nullptr && root->right == nullptr)
{
return count == 0;
}
if(root->left)
{
count -= root->left->val;
if(traversal(root->left, count))
{
return true;
}
count += root->left->val;
}
if(root->right)
{
count -= root->right->val;
if(traversal(root->right, count))
{
return true;
}
count += root->right->val;
}
return false;
}
bool hasPathSum(TreeNode
root, int targetSum)
{
int sum = 0;
if(root)
{
targetSum -= root->val;
return traversal(root, targetSum);
}
return false;
}
};

从中序遍历和后序遍历构造二叉树

class Solution
{
public:
TreeNode *traversal(vector &inorder, vector &postorder)
{
if(postorder.empty())
{
return nullptr;
}
int rootvalue = postorder[postorder.size()-1];
TreeNode *root = new TreeNode(rootvalue);
if(postorder.size() == 1)
{
return root;
}
int i = 0;
for(; i < inorder.size(); ++i)
{
if(inorder[i] == rootvalue)
{
break;
}
}
vector left1(inorder.begin(), inorder.begin() + i);
++i;
vector right1(inorder.begin()+i, inorder.end());
vector left2(postorder.begin()+0, postorder.begin()+left1.size());
vector right2(postorder.begin()+left1.size(), postorder.end()-1);
root->left = traversal(left1, left2);
root->right = traversal(right1, right2);
return root;

}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder)
{
    return traversal(inorder, postorder);
}

};

标签:return,int,traversal,depth,day13,打卡,root,postorder
From: https://www.cnblogs.com/pwangikun/p/18389523

相关文章

  • day12打卡
    平衡二叉树classSolution{public:intgetheight(TreeNode*root){if(root==nullptr){return0;}intleft=getheight(root->left);intright=getheight(root->right);return1+max(left,right);}boolisBalanced(TreeNode*root){if(root==nullptr)......
  • day11打卡
    翻转二叉树classSolution{public:voidtraversal(TreeNoderoot){if(root==nullptr){return;}traversal(root->left);traversal(root->right);TreeNodetmp=root->left;root->left=root->right;root->right=tmp;}TreeNodeinvertTree(T......
  • day9打卡
    用栈实现队列classMyQueue{public:MyQueue(){}voidpush(intx){stIn.push(x);}intpop(){inta;while(!stIn.empty()){a=stIn.top();stIn.pop();stOut.push(a);}a=stOut.top();stOut.pop();while(!stOut.empty()){intb=stOut.top();stOut.pop();......
  • day8打卡
    反转字符串中的单词classSolution{public:stringreverseWords(strings){intslow=0;inti=0;while(i<s.size()){if(s[i]!=''){if(slow!=0){s[slow++]='';}while(i<s.size()&&s[i]!=''){s[slow+......
  • day7打卡
    反转字符串利用双指针不断向中间靠拢,交换数据classSolution{public:voidreverseString(vector&s){intleft=0;intright=s.size()-1;while(left<right){chartmp='\0';tmp=s[left];s[left++]=s[right];s[right--]=tmp;}}};反转字符串II......
  • day13: 第六章 二叉树part01 |二叉树的前序遍历,后序遍历,中序遍历,(递归。层序(广度)跟
    二叉树递归三部曲:1.确定递归函数的参数和返回值。2.确定终止条件3.确定单层递归的逻辑144.二叉树的前序遍历:中左右,递归:classSolution{publicList<Integer>preorderTraversal(TreeNoderoot){List<Integer>res=newArrayList<Integer>();p......
  • 代码随想录训练营 Day42打卡 动态规划 part09 188.买卖股票的最佳时机IV 309. 最佳买
    代码随想录训练营Day42打卡动态规划part09一、力扣188.买卖股票的最佳时机IV给你一个整数数组prices和一个整数k,其中prices[i]是某支给定的股票在第i天的价格。设计一个算法来计算你所能获取的最大利润。你最多可以完成k笔交易。也就是说,你最多可以买k次......
  • Datawhale X 李宏毅苹果书AI夏令营 Task1打卡
    3.1局部极小值与鞍点3.1.1临界点及其分类参数对于损失函数的微分为零时,就无法进一步优化了,训练即停止了。所以我们把这些梯度为零的点统称为临界点。临界点可以分为两类:极值点(局部极小值)和鞍点。鞍点就是指那些梯度为零但不是局部极小值或者局部极大值的点,因为其在损失......
  • 代码训练营 Day13 | 递归遍历 | 层序遍历
    144. 二叉树的前序遍历递归思路:确定递归函数的参数和返回值确定递归函数的终止条件确定单层递归的逻辑前序遍历顺序:中左右#Definitionforabinarytreenode.#classTreeNode(object):#def__init__(self,val=0,left=None,right=None):#......
  • 企业微信如何远程打卡,免费
    现在一些定位软件不好用或者要收费,那么如何能够很好的免费实现远程打开呢?首先需要一个不用的旧手机,一直放在公司里,然后拿自己常用手机远程操作来实现,具体步骤如下:旧手机需要打开开发者模式,然后打开屏幕常亮保证不会锁屏;公司电脑下载scrcpy用来操作连接的旧手机;下载地址......