lc.199 从右到左层序遍历
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值 思路 层序遍历的时候,判断是否遍历到单层的最后面的元素,如果是,就放进result数组中,随后返回result就可以了。 C++代码: class Solution { public: vector<int> rightSideView(TreeNode* root) {...
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值 思路 层序遍历的时候,判断是否遍历到单层的最后面的元素,如果是,就放进result数组中,随后返回result就可以了。 C++代码: class Solution { public: vector<int> rightSideView(TreeNode* root) {...
class Solution { public: bool hasPathSum(TreeNode* root, int targetSum) { vector<int>vec; sum(root,targetSum,vec,0);//第四个参数是当前这条路径的路径值 //检查vec中存储的路径值是否有等于目标值的 ...
相对于102.二叉树的层序遍历,就是最后把result数组反转输出一下就可以了 reverse(result.begin(), result.end()); // 反转数组
标答 #include <iostream> #include <fstream> #include <queue> using namespace std; queue<int> Q; queue<int> q[1010]; int team[1000000]; // team[i] 表示成员 i 属于哪...
Description 给你一个字符数组 chars ,请使用下述算法压缩: 从一个空字符串 s 开始。对于 chars 中的每组 连续重复字符 : 如果这一组长度为 1 ,则将字符追加到 s 中。 否则,需要向 s 追加字符,后跟这一组的长度。 压缩后得到的字符串 s 不应该直接返回 ,需要转储到字符数组 chars 中。需要注意的是,如果组长度为 10 或 10 以上,...
class Solution { public: TreeNode* constructMaximumBinaryTree(vector<int>& nums) { return construct(nums, 0, nums.size() - 1); } TreeNode* construct(const vector<i...
class Solution { public: TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) { //如果都为空 if(!root1&&!root2){ return nullptr; } //如果...
TreeNode* invertTree(TreeNode* root) { if (root == NULL) return root; swap(root->left, root->right); // 中 invertTree(root->left); // 左 invertTree(r...
1)层序遍历法 可以按照普通二叉树的逻辑来求: 把层序遍历的模板稍稍修改一下,记录遍历的节点数量就可以了。 int countNodes(TreeNode* root) { if(!root){return 0;} queue<TreeNode*>que; que.push(root); int...
class Solution { public: int minDepth(TreeNode* root) { if(root == nullptr) return 0; int m1 = minDepth(root->left);//简便记一下 int m2 = minDepth(root->r...