
二叉树
永远的EMT
每天时刻保持超越自我的意识
展开
-
【PAT】1020. Tree Traversals
考查点:二叉树重建和BFS思路:根据后序和中序重建二叉树,可用先序遍历方法递归重建二叉树#define LOCAL#include #include #include #include #include #include #include #include #include #include #define FOR(i, x, y) for(int i = x; i原创 2017-02-11 23:43:45 · 221 阅读 · 0 评论 -
二叉树遍历的非递归总结
本文主要总结先序,中序,后序的非递归,基本思路都是用stack进行先序的非递归,两种写法,一种是注意加入左右孩子时判断是否为空,主要注意先加入右孩子保证左孩子先出来void preOrderIter(TreeNode *root){ if (root == nullptr) return; stack<TreeNode *> s; ...原创 2018-10-20 21:42:44 · 413 阅读 · 0 评论 -
【LeetCode】Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.Example:Input: [1,null,2,3] 1 \ 2 / 3Output: [3,2,1]题解:用非递归方式给出二叉树的后序遍历序列,正常想法直接用个栈但是后序是L-R-root的顺序...原创 2018-10-20 19:41:44 · 191 阅读 · 0 评论 -
【LeetCode】Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tre...原创 2018-10-20 18:43:00 · 168 阅读 · 0 评论 -
【LeetCode】Construct Binary Tree from Inorder and Postorder Traversal
题解:就是用中序和后序重建二叉树保证每个点不重复值上这里顺便列下中序和先序的代码,思路都一样用map存储可以节省查找时间,注意边界处理class Solution {private: unordered_map<int, int> inm; // inorder map [inorder[i], i]public: TreeNode* build...原创 2018-10-20 16:00:46 · 164 阅读 · 0 评论 -
【LeetCode】Unique Binary Search Trees II
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.Example:Input: 3Output:[ [1,null,3,2], [3,2,null,1], [3,1,null,null,2], [2,1,3...原创 2018-10-22 14:23:38 · 311 阅读 · 0 评论 -
【LeetCode】100. Same Tree
题解:二叉树的遍历,dfsbool isSameTree(TreeNode* p, TreeNode* q) { if(p==NULL||q==NULL)return p==q; return p->val==q->val&&isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);}原创 2017-08-24 15:28:22 · 277 阅读 · 0 评论 -
【PAT】1119. Pre- and Post-order Traversals
考查点:二叉树的遍历思路及提交情况:第一次各种格式错误和错误,格式错误居然是最后要换行。。第一次是判断是否唯一时i——后序遍历中第一个等于先序序列左子树根节点的点位置应该在后续序列的左边界前因为本来就是i==postR-1的,如果越界了应该排除,此题思路就是先序遍历的思路重建二叉树,关键在于维护各个序列的边界值#define LOCAL#include #include #inclu原创 2017-02-25 00:03:56 · 430 阅读 · 0 评论 -
【PAT】1110. Complete Binary Tree
考查点:二叉树,完全二叉树,DFS思路及提交情况:第一次输入接受字符时没考虑getchar接受换行,第二次完全二叉树下标没有从1开始,最后ans记录二叉树节点的数组n-1没有修改为n,完全改好后一直段错误实际上是程序错误,就是接收数据时出错了,只接受字符,所以节点是二位数时输入就会溢出出现段错误,以后段错误要记得可能是输入有问题,还有不要理所当然从样例看出一定是接收字符,有可能是两位数。#原创 2017-02-23 23:53:17 · 351 阅读 · 0 评论 -
【PAT】1064. Complete Binary Search Tree
考查点:BST,二叉树的遍历思路:由于是BST所以利用中序序列是递增序列可以得到CBT数组,这里用数组保存二叉树#define LOCAL#include #include #include #include #include #include #include #include #include #include #include #define FOR(i, x原创 2017-02-13 00:33:49 · 202 阅读 · 0 评论 -
【PAT】1043. Is It a Binary Search Tree
考查点:BST的插入,二叉树的遍历思路:题中的镜像树只要遍历的时候交换左右子树的顺序即可,此题用指针表示方便,另外直接用向量存放遍历序列这样比较时候直接用==即可提交情况:这里的后序遍历如果是镜像树要输出镜像树的后序,注意理解题意#define LOCAL#include #include #include #include #include #include #incl原创 2017-02-12 23:34:54 · 235 阅读 · 0 评论 -
【PAT】1099. Build A Binary Search Tree
考查点:BST,BFS,中序遍历#define LOCAL#include #include #include #include #include #include #include #include #include #include #include #define FOR(i, x, y) for(int i = x; i < y; i++)#define rFO原创 2017-02-13 10:39:54 · 249 阅读 · 0 评论 -
【PAT】1102. Invert a Binary Tree
考查点:静态数组表示二叉树,中序遍历,BFS思路:这里不要硬性使用指针表示二叉树,根据输入条件显然用数组表示很方便,而且同样可以进行中序遍历和bfs,题目中的翻转只是幌子,只要接收数据时候左右子树逆序接受就行#define LOCAL#include #include #include #include #include #include #include #include原创 2017-02-12 12:19:30 · 341 阅读 · 0 评论 -
【PAT】1086. Tree Traversals Again
考查点:二叉树重建,后序遍历#define LOCAL#include #include #include #include #include #include #include #include #include #include #include #define FOR(i, x, y) for(int i = x; i < y; i++)#define rFO原创 2017-02-12 00:13:46 · 297 阅读 · 0 评论 -
【LeetCode】Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key. The ...原创 2018-10-21 22:49:10 · 167 阅读 · 0 评论