二叉树学习小结(序列化和反序列化、二叉树遍历、二叉树镜像、二叉搜索树、二叉树子结构等)

来源:互联网 发布:人工智能用go语言开发 编辑:程序博客网 时间:2024/06/05 02:28

二叉树

[cpp] view plain copy
  1. struct TreeNode {  
  2.     int val;  
  3.     struct TreeNode *left;  
  4.     struct TreeNode *right;  
  5.     TreeNode(int x) : val(x), left(NULL), right(NULL) {}  
  6. };  

二叉树的序列化及反序列化

序列化二叉树

[cpp] view plain copy
  1. //==================================================  
  2. // 序列化二叉树  
  3. //==================================================  
  4. void serializeBTree(TreeNode* pRoot, ostream& out, char mark, char separator) {  
  5.     if(NULL == pRoot) {  
  6.         out << mark << separator;  
  7.         return;  
  8.     }  
  9.     out << pRoot->val << separator;  
  10.     serializeBTree(pRoot->left, out, mark, separator);  
  11.     serializeBTree(pRoot->right, out, mark, separator);  
  12. }  
  13. /* 序列化二叉树 
  14.  * pRoot:要序列化的二叉树 
  15.  * mark:叶子节点下的 NULL 指针标记符(默认为 #) 
  16.  * separator:分隔符(默认为空格) 
  17.  */  
  18. string Serialize(TreeNode *pRoot, char mark, char separator) {//【序列化二叉树】  
  19.     ostringstream os;  
  20.     if(NULL != pRoot) {  
  21.         serializeBTree(pRoot, os, mark, separator);  
  22.     }  
  23.     return os.str();  
  24. }  

反序列化二叉树

[cpp] view plain copy
  1. //==================================================  
  2. // 反序列化二叉树  
  3. //==================================================  
  4. // 使用流  
  5. bool readStream(istream& in, int& num, char mark, char separator) {  
  6.     if(' ' == separator) {// 空格分割的情况  
  7.         string str;  
  8.         in >> str;  
  9.         if("#" == str)  
  10.             return false;  
  11.         num = atoi(str.c_str());  
  12.         return true;  
  13.     } else {// 其他字符分割的情况  
  14.         char ch;  
  15.         string s = "";  
  16.         in >> ch;  
  17.         while(ch != separator) {  
  18.             s += ch;  
  19.             in >> ch;  
  20.         }  
  21.         if(s[0] != mark) {  
  22.             num = atoi(s.c_str());  
  23.             return true;  
  24.         }  
  25.         return false;  
  26.     }  
  27. }  
  28. void deserializeBTree(TreeNode* &pRoot, istream& in, char mark, char separator) {// 使用流  
  29.     int num;  
  30.     if(readStream(in, num, mark, separator)) {  
  31.         pRoot = new TreeNode(num);  
  32.         deserializeBTree(pRoot->left, in, mark, separator);  
  33.         deserializeBTree(pRoot->right, in, mark, separator);  
  34.     }  
  35. }  
  36. // 使用 string  
  37. bool readString(string& str, int& num, char mark, char separator) {  
  38.     string::size_type index = str.find_first_of(separator);  
  39.     if(string::npos != index) {  
  40.         string s = str.substr(0, index);  
  41.         str = str.substr(index+1);  
  42.         if(s[0] != mark) {  
  43.             num = atoi(s.c_str());  
  44.             return true;  
  45.         }  
  46.     }  
  47.     return false;  
  48. }  
  49. void deserializeBTree(TreeNode* &pRoot, string& str, char mark, char separator) {// 使用 string  
  50.     int num;  
  51.     if(readString(str, num, mark, separator)) {  
  52.         pRoot = new TreeNode(num);  
  53.         deserializeBTree(pRoot->left, str, mark, separator);  
  54.         deserializeBTree(pRoot->right, str, mark, separator);  
  55.     }  
  56. }  
  57. /* 反序列化二叉树 
  58.  * str:string 型的序列 
  59.  * mark:叶子节点下的 NULL 指针标记符(默认为 #) 
  60.  * separator:分隔符(默认为空格) 
  61.  */  
  62. TreeNode* Deserialize(string sequence, char mark, char separator){//【反序列化二叉树】  
  63.     TreeNode* pRoot = NULL;  
  64.   
  65. //    istringstream in(sequence);  
  66. //    deserializeBTree(pRoot, in, mark, separator);// 使用流  
  67.   
  68.     deserializeBTree(pRoot, sequence, mark, separator);// 使用 string  
  69.   
  70.     return pRoot;  
  71. }  

二叉树的遍历(递归)

[cpp] view plain copy
  1. //==================================  
  2. //        二叉树遍历(递归)  
  3. //==================================  
  4. void PreorderTravel(TreeNode *pRoot) {// 【先序遍历(递归)】  
  5.     if(pRoot) {  
  6.         cout << pRoot->val << " ";  
  7.         PreorderTravel(pRoot->left);  
  8.         PreorderTravel(pRoot->right);  
  9.     }  
  10. }  
  11.   
  12. void InorderTravel(TreeNode *pRoot) {// 【中序遍历(递归)】  
  13.     if(pRoot) {  
  14.         InorderTravel(pRoot->left);  
  15.         cout << pRoot->val << " ";  
  16.         InorderTravel(pRoot->right);  
  17.     }  
  18. }  
  19.   
  20. void PostorderTravel(TreeNode *pRoot) {// 【后序遍历(递归)】  
  21.     if(pRoot) {  
  22.         PostorderTravel(pRoot->left);  
  23.         PostorderTravel(pRoot->right);  
  24.         cout << pRoot->val << " ";  
  25.     }  
  26. }  

二叉树的遍历(非递归)

[cpp] view plain copy
  1. //==================================  
  2. //        二叉树遍历(非递归)  
  3. //==================================  
  4. void PreorderNonRecursive(TreeNode *pRoot) { // 【前序遍历(非递归)】  
  5.     stack<TreeNode*> s;  
  6.     TreeNode *p = pRoot;  
  7.     while(p != NULL || !s.empty()) {  
  8.         while(p != NULL) {  
  9.             cout << p->val << " ";// 根  
  10.             s.push(p);  
  11.             p = p->left;// 左  
  12.         }  
  13.         if(!s.empty()) {  
  14.             p = s.top();  
  15.             s.pop();  
  16.             p = p->right;// 右  
  17.         }  
  18.     }  
  19. }  
  20.   
  21. void InorderNonRecursive(TreeNode *pRoot) { // 【中序遍历(非递归)】  
  22.     stack<TreeNode*> s;  
  23.     TreeNode *p = pRoot;  
  24.     while(p != NULL || !s.empty()) {  
  25.         while(p != NULL) {  
  26.             s.push(p);  
  27.             p = p->left;// 左  
  28.         }  
  29.         if(!s.empty()) {  
  30.             p = s.top();  
  31.             cout << p->val << " ";// 根  
  32.             s.pop();  
  33.             p = p->right;// 右  
  34.         }  
  35.     }  
  36. }  
  37. /* 后序遍历:要保证根结点在左孩子和右孩子访问之后才能访问,因此对于任一结点 p,先将其入栈。如果P不存在左孩子和右孩子,则可以直接访问它; 
  38.  * 或者 p 存在左孩子或者右孩子,但是其左孩子和右孩子都已被访问过了,则同样可以直接访问该结点。 
  39.  * 若非上述两种情况,则将 p 的右孩子和左孩子依次入栈,这样就保证了每次取栈顶元素的时候,左孩子在右孩子前面被访问,左孩子和右孩子都在根结点前面被访问。*/  
  40. void PostorderNonRecursive(TreeNode *pRoot) { // 【后序遍历(非递归)】  
  41.     stack<TreeNode*> s;  
  42.     TreeNode *cur;// 当前结点  
  43.     TreeNode *pre = NULL;// 前一次访问的结点  
  44.     s.push(pRoot);  
  45.     while(!s.empty()) {  
  46.         cur = s.top();  
  47.         if((cur->left == NULL && cur->right == NULL) || // 如果当前结点没有孩子结点  
  48.                 (pre != NULL && (pre == cur->left || pre == cur->right))// 如果有孩子,因为孩子们是先入栈的,只要有一个孩子访问了,肯定是孩子们都被访问了(因为他们比当前结点先入栈的)  
  49.           ) {  
  50.             cout << cur->val << " ";  
  51.             s.pop();  
  52.             pre = cur;  
  53.         } else {  
  54.             if(cur->right != NULL)// 右孩子先入栈,这样,每次取栈顶元素的时候,左孩子在右孩子前面被访问  
  55.                 s.push(cur->right);  
  56.             if(cur->left != NULL)  
  57.                 s.push(cur->left);  
  58.         }  
  59.     }  
  60. }  

按层打印二叉树

从上往下打印二叉树(层序遍历不分行)

[cpp] view plain copy
  1. vector<int> TravelFromTopToBottom(TreeNode *pRoot) {// 【从上往下打印二叉树】  
  2.     vector<int> result;  
  3.     if(NULL == pRoot) return result;  
  4.     queue<TreeNode*> myQueue;  
  5.     myQueue.push(pRoot);  
  6.     while(!myQueue.empty()) {  
  7.         TreeNode *p = myQueue.front();  
  8.         result.push_back(p->val);  
  9.         myQueue.pop();  
  10.         if(p->left)  
  11.             myQueue.push(p->left);  
  12.         if(p->right)  
  13.             myQueue.push(p->right);  
  14.     }  
  15.     return result;  
  16. }  

把二叉树打印成多行(层序遍历分行)

[cpp] view plain copy
  1. vector<vector<int> > LevelTravel(TreeNode* pRoot) { // 【把二叉树打印成多行】  
  2.     vector<vector<int> > result;  
  3.     if(pRoot != NULL) {  
  4.         queue<TreeNode*> q;  
  5.         q.push(pRoot);  
  6.         int levelWith = 0;  
  7.         int n = 1;  
  8.         vector<int> v;  
  9.         while(!q.empty()) {  
  10.             TreeNode* p = q.front();  
  11.             v.push_back(p->val);  
  12.             if(p->left) {  
  13.                 q.push(p->left);  
  14.                 ++levelWith;  
  15.             }  
  16.             if(p->right) {  
  17.                 q.push(p->right);  
  18.                 ++levelWith;  
  19.             }  
  20.             q.pop();  
  21.             --n;  
  22.             if(0 == n) {  
  23.                 result.push_back(v);  
  24.                 v.resize(0);  
  25.                 n = levelWith;  
  26.                 levelWith = 0;  
  27.             }  
  28.         }  
  29.     }  
  30.     return result;  
  31. }  

按之字形顺序打印二叉树

[cpp] view plain copy
  1. vector<vector<int> > ZigzagTravel(TreeNode* pRoot) {// 【按之字形顺序打印二叉树】  
  2.     vector<vector<int> > result;  
  3.     if(NULL == pRoot) return result;  
  4.     stack<TreeNode*> s1/*从右到左压入*/, s2/*从左到右压入*/;  
  5.     s1.push(pRoot);  
  6.     vector<int> vec;  
  7.     while(!s1.empty() || !s2.empty()) {  
  8.         while(!s1.empty()) {  
  9.             TreeNode* node = s1.top();  
  10.             vec.push_back(node->val);  
  11.             if(node->left)  
  12.                 s2.push(node->left);  
  13.             if(node->right)  
  14.                 s2.push(node->right);  
  15.             s1.pop();  
  16.         }  
  17.         result.push_back(vec);  
  18.         vec.resize(0);  
  19.         while(!s2.empty()) {  
  20.             TreeNode* node = s2.top();  
  21.             vec.push_back(node->val);  
  22.             if(node->right)  
  23.                 s1.push(node->right);  
  24.             if(node->left)  
  25.                 s1.push(node->left);  
  26.             s2.pop();  
  27.         }  
  28.         result.push_back(vec);  
  29.         vec.resize(0);  
  30.     }  
  31.     return result;  
  32. }  

重建二叉树

根据前序和中序重建二叉树

[cpp] view plain copy
  1. TreeNode* constrcutBT(const vector<int>& pre, vector<int>::size_type preLow, vector<int>::size_type preHigh,  
  2.                       const vector<int>& in, vector<int>::size_type inLow, vector<int>::size_type inHigh) {  
  3.     // 前序的第一个是根  
  4.     int rootValue = pre[preLow];  
  5.     TreeNode* tree = new TreeNode(rootValue);  
  6.   
  7.     if(0 == preHigh-preLow && 0 == inHigh-inLow && pre[preLow] == in[inLow])  
  8.         return tree;  
  9.   
  10.     // 在中序里面找到这个根的位置  
  11.     vector<int>::size_type i = inLow;  
  12.     for(; i != inHigh; i++) {  
  13.         if(rootValue == in[i])  
  14.             break;  
  15.     }  
  16.   
  17.     if(i > inLow) {// 重建左子树  
  18.         vector<int>::size_type in_L_Low = inLow, in_L_High = i - 1;// 左子树的中序起始点  
  19.         vector<int>::size_type pre_L_Low = preLow + 1, pre_L_High = preLow + (i - inLow);// 左子树的前序起始点  
  20.         tree->left = constrcutBT(pre, pre_L_Low, pre_L_High, in, in_L_Low, in_L_High);  
  21.     }  
  22.     if(i < inHigh) {// 重建右子树  
  23.         vector<int>::size_type in_R_Low = i + 1, in_R_High = inHigh;// 右子树的中序起始点  
  24.         vector<int>::size_type pre_R_Low = preLow + (i - inLow) + 1, pre_R_High = preHigh;// 右子树的前序起始点  
  25.         tree->right = constrcutBT(pre, pre_R_Low, pre_R_High, in, in_R_Low, in_R_High);  
  26.     }  
  27.     return tree;  
  28. }  
  29. struct TreeNode* ReConstructBinaryTree(vector<int> pre, vector<int> in) {// 【根据前序和中序重建二叉树】  
  30.     if(pre.empty() || in.empty() || pre.size() != in.size())  
  31.         return NULL;  
  32.     return constrcutBT(pre, 0, pre.size()-1, in, 0, in.size()-1);  
  33. }  

判断二叉树是否是对称二叉树

[cpp] view plain copy
  1. bool isSymm(TreeNode* pRoot1, TreeNode* pRoot2) {  
  2.     if(NULL == pRoot1 && NULL == pRoot2) return true;  
  3.     if(NULL == pRoot1 || NULL == pRoot2) return false;  
  4.     if(pRoot1->val != pRoot2->val) return false;  
  5.     return (isSymm(pRoot1->left, pRoot2->right) && isSymm(pRoot1->right, pRoot2->left));  
  6. }  
  7. bool IsSymmetrical(TreeNode* pRoot) {// 【对称二叉树】  
  8.     return isSymm(pRoot, pRoot);  
  9. }  

二叉树的镜像(对换左右子树)

[cpp] view plain copy
  1. void Mirror(TreeNode *pRoot) {// 【二叉树的镜像(对换左右子树)】  
  2.     if(pRoot != NULL) {  
  3.         TreeNode *p = pRoot->left;  
  4.         pRoot->left = pRoot->right;  
  5.         pRoot->right = p;  
  6.         Mirror(pRoot->left);  
  7.         Mirror(pRoot->right);  
  8.     }  
  9. }  

判断树的子结构

[cpp] view plain copy
  1. bool hasST(TreeNode *pRoot1, TreeNode *pRoot2) {// 判断树1中以此节点为根节点的子树是不是包含数2一样的结构  
  2.     if(NULL == pRoot2) return true;// 递归终止条件  
  3.     if(NULL == pRoot1) return false;  
  4.     if(pRoot1->val != pRoot2->val) return false;  
  5.     return (hasST(pRoot1->left, pRoot2->left) && hasST(pRoot1->right, pRoot2->right));  
  6. }  
  7. bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {// 【判断树 pRoot2 是不是树 pRoot1 的子结构】  
  8.     bool flag = false;  
  9.     if(pRoot1 != NULL && pRoot2 != NULL) {  
  10.         if(pRoot1->val == pRoot2->val)// 在树1中找到和树2的根节点的值一样的节点  
  11.             flag = hasST(pRoot1, pRoot2);// 判断树1中以此节点为根节点的子树是不是包含数2一样的结构  
  12.         if(!flag)  
  13.             flag = HasSubtree(pRoot1->left, pRoot2);// 在左子树中查找  
  14.         if(!flag)  
  15.             flag = HasSubtree(pRoot1->right, pRoot2);// 在右子树中查找  
  16.     }  
  17.     return flag;  
  18. }  

判断二叉树是否是平衡二叉树

[cpp] view plain copy
  1. bool isBalanced(TreeNode* pRoot, int& depth) {  
  2.     if(!pRoot) {  
  3.         depth = 0;  
  4.         return true;  
  5.     }  
  6.     int leftDepth, rightDepth;  
  7.     // 如果左右子树是平衡的,则计算当前节点作为根节点的树是否是平衡的  
  8.     if(isBalanced(pRoot->left, leftDepth) && isBalanced(pRoot->right, rightDepth)) {  
  9.         int d = abs(leftDepth - rightDepth);  
  10.         if(d <= 1) {  
  11.             depth = leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;  
  12.             return true;  
  13.         }  
  14.     }  
  15.     return false;  
  16. }  
  17. bool IsBalanced(TreeNode* pRoot) {// 【判断二叉树是否是平衡二叉树】  
  18. //    // 方案一  
  19. //    if(!pRoot)  
  20. //        return true;  
  21. //    int leftDepth = TreeDepth(pRoot->left);// 计算左子树深度  
  22. //    int rightDepth = TreeDepth(pRoot->right);// 计算右子树深度  
  23. //    int d = abs(leftDepth - rightDepth);// 左右子树深度差  
  24. //    if(d > 1)// 如果深度差超过 1 则不是平衡二叉树  
  25. //        return false;  
  26. //    return IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right);  
  27.   
  28.     // 方案二(方案一中一个结点会被重复遍历多次,效率不高。这里方案二采用后序遍历的方式,每个节点只访问一次)  
  29.     int depth;  
  30.     return isBalanced(pRoot, depth);  
  31. }  

二叉树的深度

[cpp] view plain copy
  1. //void getTreeDepth(TreeNode* p, int& depth, int& maxDepth) {  
  2. //    if(p) {  
  3. //        depth++;  
  4. //        if(!p->left && !p->right) {// 如果到了叶子节点,则更新深度值  
  5. //            if(depth > maxDepth)  
  6. //                maxDepth = depth;  
  7. //        }  
  8. //        else {  
  9. //            if(p->left) {  
  10. //                getTreeDepth(p->left, depth, maxDepth);  
  11. //                depth--;  
  12. //            }  
  13. //            if(p->right)  
  14. //                getTreeDepth(p->right, depth, maxDepth);  
  15. //        }  
  16. //    }  
  17. //}  
  18. //int TreeDepth(TreeNode* pRoot) {//【二叉树的深度】方案一  
  19. //    int depth = 0, maxDepth = 0;  
  20. //    getTreeDepth(pRoot, depth, maxDepth);  
  21. //    return maxDepth;  
  22. //}  
  23. int TreeDepth(TreeNode* pRoot) {//【二叉树的深度】方案二  
  24.     if(!pRoot) return 0;  
  25.     int leftDepth = TreeDepth(pRoot->left);// 计算左子树深度  
  26.     int rightDepth = TreeDepth(pRoot->right);// 计算右子树深度  
  27.     return (leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1);// 树的深度为较大者深度 + 1  
  28. }  

二叉树中和为某一值的路径

[cpp] view plain copy
  1. void findPath(TreeNode* root, int& curSum, const int& expectNumber, vector<int>& path, vector<vector<int> >& paths) {  
  2.     if(root) {  
  3.         curSum += root->val;  
  4.         path.push_back(root->val);  
  5.         if(NULL == root->left && NULL == root->right && curSum == expectNumber)  
  6.             paths.push_back(path);  
  7.         if(root->left)  
  8.             findPath(root->left, curSum, expectNumber, path, paths);  
  9.         if(root->right)  
  10.             findPath(root->right, curSum, expectNumber, path, paths);  
  11.         curSum -= root->val;  
  12.         path.pop_back();  
  13.     }  
  14. }  
  15. vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {// 【二叉树中和为某一值的路径】  
  16.     vector<vector<int> > paths;  
  17.     if(NULL == root) return paths;  
  18.     int curSum = 0;  
  19.     vector<int> path;  
  20.     findPath(root, curSum, expectNumber, path, paths);  
  21.     return paths;  
  22. }  

二叉搜索树

判断一个序列是不是二叉搜索树的后序遍历序列

[cpp] view plain copy
  1. bool VerifySquenceOfBST(vector<int> sequence) {// 【判断一个序列是不是二叉搜索树的后序遍历序列】  
  2.     if(sequence.empty()) return false;  
  3.     int rootValue = sequence[sequence.size()-1];// 根节点  
  4.     vector<int>::size_type length = 0, i = 0;  
  5.     for(; i < sequence.size()-1; i++)// 寻找根节点左右子树分割点  
  6.         if(sequence[i] > rootValue)  
  7.             break;  
  8.     length = i;// 左子树节点数  
  9.     for(++i; i < sequence.size()-1; i++)// 判断右子树节点中,是否有小于根节点值的  
  10.         if(sequence[i] < rootValue)  
  11.             return false;  
  12.     bool L_flag = true, R_flag = true;  
  13.     if(length > 0) {// 递归判断左子树  
  14.         vector<int> vecL(sequence.begin(), sequence.begin()+length);  
  15.         L_flag = VerifySquenceOfBST(vecL);  
  16.     }  
  17.     if(length < sequence.size()-1) {// 递归判断右子树  
  18.         vector<int> vecR(sequence.begin()+length, sequence.end()-1);  
  19.         R_flag = VerifySquenceOfBST(vecR);  
  20.     }  
  21.     return (L_flag && R_flag);  
  22. }  

二叉搜索树转为双向链表

[cpp] view plain copy
  1. void convertTree2List(TreeNode* p, TreeNode* &pList) {  
  2.     if(p) {  
  3.         if(p->left)  
  4.             convertTree2List(p->left, pList);// 转换左子树  
  5.   
  6.         p->left = pList;// 将此根节点左边指向双向链表的最右边  
  7.         if(pList)  
  8.             pList->right = p;// 双向链表的最右边的节点指向此根节点  
  9.         pList = p;// 更新 pList 指向双向链表的最右边  
  10.   
  11.         if(p->right)  
  12.             convertTree2List(p->right, pList);// 转换右子树  
  13.     }  
  14. }  
  15. TreeNode* Convert(TreeNode* pRootOfTree) {// 【二叉搜索树转为双向链表】  
  16.     if(pRootOfTree) return NULL;  
  17.     TreeNode *pList/*指向双向链表最右边那个节点*/ = NULL, *pListHead/*双向链表*/ = NULL;  
  18.     convertTree2List(pRootOfTree, pList);  
  19.     pListHead = pList;  
  20.     while(pListHead && pListHead->left)// 因为 pList 指向的是双向链表的最右边,所以反向遍历到最左边得到表头  
  21.         pListHead = pListHead->left;  
  22.     return pListHead;  
  23. }  

二叉搜索树的第 k 个结点

给定一颗二叉搜索树,请找出其中的第k小的结点。例如, 5 3 2 # # 4 # # 7 6 # # 8 # #,按结点数值大小顺序第三个结点的值为 4 
[cpp] view plain copy
  1. bool FindKthOfBST(TreeNode* pRoot, int& k, int& val) {//【查找二叉搜索树的第 k 小的结点,若找到返回 true,并用 val 存放这个结点的值】  
  2.     // 用中序遍历即可从小到大访问二叉搜索树的结点  
  3.   
  4.     bool isFind = false;  
  5.     if(pRoot != NULL) {  
  6.         // 左  
  7.         isFind = FindKthOfBST(pRoot->left, k, val);  
  8.         // 根  
  9.         k--;  
  10.         if(0 == k) {  
  11.             val = pRoot->val;  
  12.             isFind = true;  
  13.         }  
  14.         // 右  
  15.         if(!isFind)  
  16.             isFind = FindKthOfBST(pRoot->right, k, val);  
  17.     }  
  18.     return isFind;  
  19. }  
1 0
原创粉丝点击