二叉树
- struct TreeNode {
- int val;
- struct TreeNode *left;
- struct TreeNode *right;
- TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- };
二叉树的序列化及反序列化
序列化二叉树
-
-
-
- void serializeBTree(TreeNode* pRoot, ostream& out, char mark, char separator) {
- if(NULL == pRoot) {
- out << mark << separator;
- return;
- }
- out << pRoot->val << separator;
- serializeBTree(pRoot->left, out, mark, separator);
- serializeBTree(pRoot->right, out, mark, separator);
- }
-
-
-
-
-
- string Serialize(TreeNode *pRoot, char mark, char separator) {
- ostringstream os;
- if(NULL != pRoot) {
- serializeBTree(pRoot, os, mark, separator);
- }
- return os.str();
- }
反序列化二叉树
-
-
-
-
- bool readStream(istream& in, int& num, char mark, char separator) {
- if(' ' == separator) {
- string str;
- in >> str;
- if("#" == str)
- return false;
- num = atoi(str.c_str());
- return true;
- } else {
- char ch;
- string s = "";
- in >> ch;
- while(ch != separator) {
- s += ch;
- in >> ch;
- }
- if(s[0] != mark) {
- num = atoi(s.c_str());
- return true;
- }
- return false;
- }
- }
- void deserializeBTree(TreeNode* &pRoot, istream& in, char mark, char separator) {
- int num;
- if(readStream(in, num, mark, separator)) {
- pRoot = new TreeNode(num);
- deserializeBTree(pRoot->left, in, mark, separator);
- deserializeBTree(pRoot->right, in, mark, separator);
- }
- }
-
- bool readString(string& str, int& num, char mark, char separator) {
- string::size_type index = str.find_first_of(separator);
- if(string::npos != index) {
- string s = str.substr(0, index);
- str = str.substr(index+1);
- if(s[0] != mark) {
- num = atoi(s.c_str());
- return true;
- }
- }
- return false;
- }
- void deserializeBTree(TreeNode* &pRoot, string& str, char mark, char separator) {
- int num;
- if(readString(str, num, mark, separator)) {
- pRoot = new TreeNode(num);
- deserializeBTree(pRoot->left, str, mark, separator);
- deserializeBTree(pRoot->right, str, mark, separator);
- }
- }
-
-
-
-
-
- TreeNode* Deserialize(string sequence, char mark, char separator){
- TreeNode* pRoot = NULL;
-
-
-
-
- deserializeBTree(pRoot, sequence, mark, separator);
-
- return pRoot;
- }
二叉树的遍历(递归)
-
-
-
- void PreorderTravel(TreeNode *pRoot) {
- if(pRoot) {
- cout << pRoot->val << " ";
- PreorderTravel(pRoot->left);
- PreorderTravel(pRoot->right);
- }
- }
-
- void InorderTravel(TreeNode *pRoot) {
- if(pRoot) {
- InorderTravel(pRoot->left);
- cout << pRoot->val << " ";
- InorderTravel(pRoot->right);
- }
- }
-
- void PostorderTravel(TreeNode *pRoot) {
- if(pRoot) {
- PostorderTravel(pRoot->left);
- PostorderTravel(pRoot->right);
- cout << pRoot->val << " ";
- }
- }
二叉树的遍历(非递归)
-
-
-
- void PreorderNonRecursive(TreeNode *pRoot) {
- stack<TreeNode*> s;
- TreeNode *p = pRoot;
- while(p != NULL || !s.empty()) {
- while(p != NULL) {
- cout << p->val << " ";
- s.push(p);
- p = p->left;
- }
- if(!s.empty()) {
- p = s.top();
- s.pop();
- p = p->right;
- }
- }
- }
-
- void InorderNonRecursive(TreeNode *pRoot) {
- stack<TreeNode*> s;
- TreeNode *p = pRoot;
- while(p != NULL || !s.empty()) {
- while(p != NULL) {
- s.push(p);
- p = p->left;
- }
- if(!s.empty()) {
- p = s.top();
- cout << p->val << " ";
- s.pop();
- p = p->right;
- }
- }
- }
-
-
-
- void PostorderNonRecursive(TreeNode *pRoot) {
- stack<TreeNode*> s;
- TreeNode *cur;
- TreeNode *pre = NULL;
- s.push(pRoot);
- while(!s.empty()) {
- cur = s.top();
- if((cur->left == NULL && cur->right == NULL) ||
- (pre != NULL && (pre == cur->left || pre == cur->right))
- ) {
- cout << cur->val << " ";
- s.pop();
- pre = cur;
- } else {
- if(cur->right != NULL)
- s.push(cur->right);
- if(cur->left != NULL)
- s.push(cur->left);
- }
- }
- }
按层打印二叉树
从上往下打印二叉树(层序遍历不分行)
- vector<int> TravelFromTopToBottom(TreeNode *pRoot) {
- vector<int> result;
- if(NULL == pRoot) return result;
- queue<TreeNode*> myQueue;
- myQueue.push(pRoot);
- while(!myQueue.empty()) {
- TreeNode *p = myQueue.front();
- result.push_back(p->val);
- myQueue.pop();
- if(p->left)
- myQueue.push(p->left);
- if(p->right)
- myQueue.push(p->right);
- }
- return result;
- }
把二叉树打印成多行(层序遍历分行)
- vector<vector<int> > LevelTravel(TreeNode* pRoot) {
- vector<vector<int> > result;
- if(pRoot != NULL) {
- queue<TreeNode*> q;
- q.push(pRoot);
- int levelWith = 0;
- int n = 1;
- vector<int> v;
- while(!q.empty()) {
- TreeNode* p = q.front();
- v.push_back(p->val);
- if(p->left) {
- q.push(p->left);
- ++levelWith;
- }
- if(p->right) {
- q.push(p->right);
- ++levelWith;
- }
- q.pop();
- --n;
- if(0 == n) {
- result.push_back(v);
- v.resize(0);
- n = levelWith;
- levelWith = 0;
- }
- }
- }
- return result;
- }
按之字形顺序打印二叉树
- vector<vector<int> > ZigzagTravel(TreeNode* pRoot) {
- vector<vector<int> > result;
- if(NULL == pRoot) return result;
- stack<TreeNode*> s1, s2;
- s1.push(pRoot);
- vector<int> vec;
- while(!s1.empty() || !s2.empty()) {
- while(!s1.empty()) {
- TreeNode* node = s1.top();
- vec.push_back(node->val);
- if(node->left)
- s2.push(node->left);
- if(node->right)
- s2.push(node->right);
- s1.pop();
- }
- result.push_back(vec);
- vec.resize(0);
- while(!s2.empty()) {
- TreeNode* node = s2.top();
- vec.push_back(node->val);
- if(node->right)
- s1.push(node->right);
- if(node->left)
- s1.push(node->left);
- s2.pop();
- }
- result.push_back(vec);
- vec.resize(0);
- }
- return result;
- }
重建二叉树
根据前序和中序重建二叉树
- TreeNode* constrcutBT(const vector<int>& pre, vector<int>::size_type preLow, vector<int>::size_type preHigh,
- const vector<int>& in, vector<int>::size_type inLow, vector<int>::size_type inHigh) {
-
- int rootValue = pre[preLow];
- TreeNode* tree = new TreeNode(rootValue);
-
- if(0 == preHigh-preLow && 0 == inHigh-inLow && pre[preLow] == in[inLow])
- return tree;
-
-
- vector<int>::size_type i = inLow;
- for(; i != inHigh; i++) {
- if(rootValue == in[i])
- break;
- }
-
- if(i > inLow) {
- vector<int>::size_type in_L_Low = inLow, in_L_High = i - 1;
- vector<int>::size_type pre_L_Low = preLow + 1, pre_L_High = preLow + (i - inLow);
- tree->left = constrcutBT(pre, pre_L_Low, pre_L_High, in, in_L_Low, in_L_High);
- }
- if(i < inHigh) {
- vector<int>::size_type in_R_Low = i + 1, in_R_High = inHigh;
- vector<int>::size_type pre_R_Low = preLow + (i - inLow) + 1, pre_R_High = preHigh;
- tree->right = constrcutBT(pre, pre_R_Low, pre_R_High, in, in_R_Low, in_R_High);
- }
- return tree;
- }
- struct TreeNode* ReConstructBinaryTree(vector<int> pre, vector<int> in) {
- if(pre.empty() || in.empty() || pre.size() != in.size())
- return NULL;
- return constrcutBT(pre, 0, pre.size()-1, in, 0, in.size()-1);
- }
判断二叉树是否是对称二叉树
- bool isSymm(TreeNode* pRoot1, TreeNode* pRoot2) {
- if(NULL == pRoot1 && NULL == pRoot2) return true;
- if(NULL == pRoot1 || NULL == pRoot2) return false;
- if(pRoot1->val != pRoot2->val) return false;
- return (isSymm(pRoot1->left, pRoot2->right) && isSymm(pRoot1->right, pRoot2->left));
- }
- bool IsSymmetrical(TreeNode* pRoot) {
- return isSymm(pRoot, pRoot);
- }
二叉树的镜像(对换左右子树)
- void Mirror(TreeNode *pRoot) {
- if(pRoot != NULL) {
- TreeNode *p = pRoot->left;
- pRoot->left = pRoot->right;
- pRoot->right = p;
- Mirror(pRoot->left);
- Mirror(pRoot->right);
- }
- }
判断树的子结构
- bool hasST(TreeNode *pRoot1, TreeNode *pRoot2) {
- if(NULL == pRoot2) return true;
- if(NULL == pRoot1) return false;
- if(pRoot1->val != pRoot2->val) return false;
- return (hasST(pRoot1->left, pRoot2->left) && hasST(pRoot1->right, pRoot2->right));
- }
- bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
- bool flag = false;
- if(pRoot1 != NULL && pRoot2 != NULL) {
- if(pRoot1->val == pRoot2->val)
- flag = hasST(pRoot1, pRoot2);
- if(!flag)
- flag = HasSubtree(pRoot1->left, pRoot2);
- if(!flag)
- flag = HasSubtree(pRoot1->right, pRoot2);
- }
- return flag;
- }
判断二叉树是否是平衡二叉树
- bool isBalanced(TreeNode* pRoot, int& depth) {
- if(!pRoot) {
- depth = 0;
- return true;
- }
- int leftDepth, rightDepth;
-
- if(isBalanced(pRoot->left, leftDepth) && isBalanced(pRoot->right, rightDepth)) {
- int d = abs(leftDepth - rightDepth);
- if(d <= 1) {
- depth = leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
- return true;
- }
- }
- return false;
- }
- bool IsBalanced(TreeNode* pRoot) {
-
-
-
-
-
-
-
-
-
-
-
- int depth;
- return isBalanced(pRoot, depth);
- }
二叉树的深度
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- int TreeDepth(TreeNode* pRoot) {
- if(!pRoot) return 0;
- int leftDepth = TreeDepth(pRoot->left);
- int rightDepth = TreeDepth(pRoot->right);
- return (leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1);
- }
二叉树中和为某一值的路径
- void findPath(TreeNode* root, int& curSum, const int& expectNumber, vector<int>& path, vector<vector<int> >& paths) {
- if(root) {
- curSum += root->val;
- path.push_back(root->val);
- if(NULL == root->left && NULL == root->right && curSum == expectNumber)
- paths.push_back(path);
- if(root->left)
- findPath(root->left, curSum, expectNumber, path, paths);
- if(root->right)
- findPath(root->right, curSum, expectNumber, path, paths);
- curSum -= root->val;
- path.pop_back();
- }
- }
- vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
- vector<vector<int> > paths;
- if(NULL == root) return paths;
- int curSum = 0;
- vector<int> path;
- findPath(root, curSum, expectNumber, path, paths);
- return paths;
- }
二叉搜索树
判断一个序列是不是二叉搜索树的后序遍历序列
- bool VerifySquenceOfBST(vector<int> sequence) {
- if(sequence.empty()) return false;
- int rootValue = sequence[sequence.size()-1];
- vector<int>::size_type length = 0, i = 0;
- for(; i < sequence.size()-1; i++)
- if(sequence[i] > rootValue)
- break;
- length = i;
- for(++i; i < sequence.size()-1; i++)
- if(sequence[i] < rootValue)
- return false;
- bool L_flag = true, R_flag = true;
- if(length > 0) {
- vector<int> vecL(sequence.begin(), sequence.begin()+length);
- L_flag = VerifySquenceOfBST(vecL);
- }
- if(length < sequence.size()-1) {
- vector<int> vecR(sequence.begin()+length, sequence.end()-1);
- R_flag = VerifySquenceOfBST(vecR);
- }
- return (L_flag && R_flag);
- }
二叉搜索树转为双向链表
- void convertTree2List(TreeNode* p, TreeNode* &pList) {
- if(p) {
- if(p->left)
- convertTree2List(p->left, pList);
-
- p->left = pList;
- if(pList)
- pList->right = p;
- pList = p;
-
- if(p->right)
- convertTree2List(p->right, pList);
- }
- }
- TreeNode* Convert(TreeNode* pRootOfTree) {
- if(pRootOfTree) return NULL;
- TreeNode *pList = NULL, *pListHead = NULL;
- convertTree2List(pRootOfTree, pList);
- pListHead = pList;
- while(pListHead && pListHead->left)
- pListHead = pListHead->left;
- return pListHead;
- }
二叉搜索树的第 k 个结点
给定一颗二叉搜索树,请找出其中的第k小的结点。例如, 5 3 2 # # 4 # # 7 6 # # 8 # #,按结点数值大小顺序第三个结点的值为 4
- bool FindKthOfBST(TreeNode* pRoot, int& k, int& val) {
-
-
- bool isFind = false;
- if(pRoot != NULL) {
-
- isFind = FindKthOfBST(pRoot->left, k, val);
-
- k--;
- if(0 == k) {
- val = pRoot->val;
- isFind = true;
- }
-
- if(!isFind)
- isFind = FindKthOfBST(pRoot->right, k, val);
- }
- return isFind;
- }