赞
踩
day15 记录代码随想录
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- /*写一下结构体链表
- struct tree{
- int val;
- tree* left;
- tree* right;
- tree(int x) : val(x),left(NULL),right(NULL) {}
- };
- */
- class Solution {
- public:
- vector<vector<int>> levelOrder(TreeNode* root) {
- vector<vector<int>> result;
- queue<TreeNode*> que;
- if(root != NULL) que.push(root);
- while(!que.empty()) {
- int size = que.size();
- vector<int> floor;
- while(size--) {
- if(que.front()->left != NULL) que.push(que.front()->left);
- if(que.front()->right != NULL) que.push(que.front()->right);
- floor.push_back(que.front()->val);
- que.pop();
- }
- result.push_back(floor);
- }
- return result;
- }
- };
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- vector<vector<int>> levelOrderBottom(TreeNode* root) {
- vector<vector<int>> result;
- queue<TreeNode*> que;
- if(root != NULL) que.push(root);
- while(!que.empty()) {
- vector<int> floor;
- int size = que.size();
- while(size--) {
- TreeNode* node;
- node = que.front();
- if( node->right != NULL) que.push(node->right);
- if( node->left != NULL) que.push(node->left);
- floor.push_back(node->val);
- que.pop();
- }
- reverse(floor.begin(),floor.end());
- result.push_back(floor);
- }
- reverse(result.begin(),result.end());
- return result;
- }
- };
给定一个二叉树的 根节点 root
,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- vector<int> rightSideView(TreeNode* root) {
- vector<int> result;
- queue<TreeNode*> que;
- if(root == NULL) return result;
- que.push(root);
- while(!que.empty()) {
- int size;
- vector<int> floor;
- size = que.size();
- while(size--) {
- TreeNode* cur = que.front();
- if(cur->right) que.push(cur->right);
- if(cur->left) que.push(cur->left);
- floor.push_back(cur->val);
- que.pop();
- }
- result.push_back(floor[0]);
- }
- return result;
- }
- };
给定一个非空二叉树的根节点 root
, 以数组的形式返回每一层节点的平均值。与实际答案相差 10-5
以内的答案可以被接受。
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- vector<double> averageOfLevels(TreeNode* root) {
- vector<double> result;
- queue<TreeNode*> que;
- if(root == NULL) return result;
- que.push(root);
- while(!que.empty()) {
- int size = que.size();
- int c = size;
- double sum = 0;
- while(size--) {
- TreeNode* cur = que.front();
- if(cur->left) que.push(cur->left);
- if(cur->right) que.push(cur->right);
- que.pop();
- sum += cur->val;
- }
- result.push_back(sum/c);
- }
- return result;
- }
- };
给定一个 N 叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。
树的序列化输入是用层序遍历,每组子节点都由 null 值分隔(参见示例)。
- /*
- // Definition for a Node.
- class Node {
- public:
- int val;
- vector<Node*> children;
- Node() {}
- Node(int _val) {
- val = _val;
- }
- Node(int _val, vector<Node*> _children) {
- val = _val;
- children = _children;
- }
- };
- */
-
- class Solution {
- public:
- vector<vector<int>> levelOrder(Node* root) {
- vector<vector<int>> result;
- if(root == NULL) return result;
- queue<Node*> que;
- que.push(root);
- while(!que.empty()) {
- vector<int> floor;
- int size = que.size();
- while(size--) {
- Node* cur = que.front();
- for(int i = 0; i < cur->children.size(); i++) {
- que.push(cur->children[i]);
- }
- floor.push_back(cur->val);
- que.pop();
- }
- result.push_back(floor);
- }
- return result;
- }
- };
给定一棵二叉树的根节点 root
,请找出该二叉树中每一层的最大值。
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- vector<int> largestValues(TreeNode* root) {
- vector<int> result;
- queue<TreeNode*> que;
- if(root == NULL) return result;
- que.push(root);
- while(!que.empty()) {
- int max = que.front()->val;
- int size = que.size();
- while(size--) {
- TreeNode* cur = que.front();
- if(cur->left) que.push(cur->left);
- if(cur->right) que.push(cur->right);
- if(cur->val > max) max = cur->val;
- que.pop();
- }
- result.push_back(max);
- }
- return result;
- }
- };
给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:
struct Node { int val; Node *left; Node *right; Node *next; }
填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL
。
初始状态下,所有 next 指针都被设置为 NULL
。
- /*
- // Definition for a Node.
- class Node {
- public:
- int val;
- Node* left;
- Node* right;
- Node* next;
- Node() : val(0), left(NULL), right(NULL), next(NULL) {}
- Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
- Node(int _val, Node* _left, Node* _right, Node* _next)
- : val(_val), left(_left), right(_right), next(_next) {}
- };
- */
-
- class Solution {
- public:
- Node* connect(Node* root) {
- if(root == NULL) return NULL;
- queue<Node*> que;
- que.push(root);
- while(!que.empty()) {
- int size = que.size();
- while(size--) {
- Node* cur = que.front();
- if(cur->left) que.push(cur->left);
- if(cur->right) que.push(cur->right);
- que.pop();
- if(size == 0)
- cur->next = NULL;
- else
- cur->next = que.front();
- }
- }
- return root;
- }
- };
给定一个二叉树:
struct Node { int val; Node *left; Node *right; Node *next; }
填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL
。
初始状态下,所有 next 指针都被设置为 NULL
。
emmm,写这个的时候发现上一题代码一样。
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- int maxDepth(TreeNode* root) {
- if(root == NULL) return 0;
- queue<TreeNode*> que;
- que.push(root);
- int depth = 0;
- while(!que.empty()) {
- int size = que.size();
- while(size--) {
- TreeNode* cur = que.front();
- if(cur->left) que.push(cur->left);
- if(cur->right) que.push(cur->right);
- que.pop();
- }
- depth++;
- }
- return depth;
-
- }
- };
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- int minDepth(TreeNode* root) {
- if(root == NULL) return 0;
- queue<TreeNode*> que;
- int depth = 0;
- que.push(root);
- while(1) {
- int size = que.size();
- depth++;
- while(size--) {
- TreeNode* cur = que.front();
- que.pop();
- if(!cur->left && !cur->right) return depth;
- else {
- if(cur->left) que.push(cur->left);
- if(cur->right) que.push(cur->right);
- }
- }
-
- }
- return 0;
- }
- };
给你一棵二叉树的根节点 root
,翻转这棵二叉树,并返回其根节点。
题目链接:226. 翻转二叉树
解题思路:
递归函数三步:传入参数、终止条件、单次循环
递归法
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- //递归
- class Solution {
- public:
- TreeNode* invertTree(TreeNode* root) {
- TreeNode* cur = root;
- if(cur == NULL) return cur;
- swap(cur->left,cur->right);
- invertTree(cur->left);
- invertTree(cur->right);
- return root;
- }
- };
迭代法
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- //迭代
- class Solution {
- public:
- TreeNode* invertTree(TreeNode* root) {
- if(root == NULL) return NULL;
- stack<TreeNode*> st;
- st.push(root);
- while(!st.empty()) {
- TreeNode* cur = st.top();
- st.pop();
- swap(cur->left,cur->right);
- if(cur->left) st.push(cur->left);
- if(cur->right) st.push(cur->right);
- }
- return root;
- }
- };
给你一个二叉树的根节点 root
, 检查它是否轴对称。
题目链接:力扣题目链接
递归法
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- bool compare(TreeNode* left, TreeNode* right) {
- if(left == NULL && right == NULL) return 1;
- else if(left != NULL && right == NULL) return 0;
- else if(left == NULL && right != NULL) return 0;
- else if(left->val != right->val) return 0;
- else {
- bool l = compare(left->left, right->right);
- bool r = compare(left->right, right->left);
- return l&&r;
- }
- }
-
- bool isSymmetric(TreeNode* root) {
- if(root == NULL) return 1;
- return compare(root->left, root->right);
- }
- };
迭代法
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- //迭代法
- class Solution {
- public:
- bool isSymmetric(TreeNode* root) {
- if(root == NULL) return 1;
- queue<TreeNode*> que;
- que.push(root->left);
- que.push(root->right);
- while(!que.empty()) {
- TreeNode* left = que.front();
- que.pop();
- TreeNode* right = que.front();
- que.pop();
- if(left == NULL && right == NULL) continue;
- else if(left == NULL || right == NULL || left->val != right->val)
- return 0;
- else
- que.push(left->left);
- que.push(right->right);
- que.push(left->right);
- que.push(right->left);
- }
- return 1;
- }
- };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。