- 博客(8)
- 收藏
- 关注
原创 二叉树先序遍历和后序遍历,从递归到迭代
二叉树的先序遍历:(以 leetcode 144. 二叉树的前序遍历 为例)空树的遍历序列为空序列二叉树先序遍历序列 = 根节点 + 左子树遍历序列 + 右子树遍历序列二叉树先序遍历递归实现:class Solution { vector<int> result; void preorder(TreeNode *root) { if (!root) return; result.push_back(root->val); preorder(root->lef
2020-07-09 10:16:50 176
原创 二叉搜索树插入操作之迭代和递归 C++简短描述
二叉搜索树插入算法描述:若二叉搜索树为空树,直接插入结点,使之成为根节点若二叉搜索树非空:若val < root->val 插入左子树(可以为空树)若val > root->val 插入右子树(可以为空树)二叉树结构体设计二叉搜索树迭代插入二叉搜索树递归插入...
2020-07-09 09:19:23 234
原创 173. 二叉搜索树迭代器
class BSTIterator {public: vector<TreeNode *> box; BSTIterator(TreeNode *root) { //把二叉树最左边的一列全部加入box while (root != NULL) { box.push_back(root); root = root->left; } } int next() { auto now = box.back(); box.pop_back(); //box最
2020-06-14 15:12:52 92
原创 Invert Binary Tree
class Solution {public: TreeNode *invertTree(TreeNode *root) { if (root == NULL) return NULL; queue<TreeNode *> q; q.push(root); while (!q.empty()) { auto now = q.front(); q.pop(); if (now->left != NULL) q.push(now->left); i.
2020-06-14 14:49:44 114
原创 C++实现二叉树先序遍历、中序遍历、后序遍历、利用先序遍历序列和中序遍历序列构造二叉树
#include <cstdio>#include <string>#include <iostream>#include <stack>using namespace std;typedef struct BiTNode { char data; BiTNode* lchild = nullptr, * rchild = nullpt...
2020-04-24 17:44:46 2086
原创 C++实现求命题公式真值表
#include <bits/stdc++.h>using namespace std;#define LMGGTQL#ifdef LMGGTQL#define AND '&'#define OR 'V'#define NOT '!'#define IFF '@'#define IF '-'inline string CharToString(char c) ...
2020-03-25 16:52:44 3740
原创 矩阵快速幂模板(重载*运算符)
#include <iostream>using namespace std;class mat { static const auto MOD = 1000000007ll, N = 3; using LL = long long; LL a[N][N]{ 0 };public:#define All(a) for(LL a = 0; a < N; a++)...
2020-02-23 11:31:39 172
原创 【POJ1426】Find The Multiple
#include <iostream>long long q[1000000]{ 0,1 };int main() { for (long long n, ans, head, tail; std::cin >> n && n; q[1] = 1) { for (q[0] = head = tail = 1; head <= tail; he...
2020-02-13 16:55:23 295 1
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人