二叉树
构建二叉树
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| typedef struct BTNode{ int data; struct BTNode *left; struct BTNode *right; }BTNode;
BTNode BinaryTree() { BTNode *T = (BTNode*)malloc(sizeof(BTNode)); T->data = 0; return T; }
BTNode MakeTree(int x, BTNode *l, BTNode *r) { BTNode *T = (BTNode*)malloc(sizeof(BTNode)); T->data = x; T->left = l; T->right = r; return T; }
|
先序非递归
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
void PreOrder(BTNode *root) { if(!root) exit(1); stack<BTNode*> stack; BTNode *cur; stack.push(root); while(!stack.empty()) { cur = stack.top(); stack.pop(); cout << cur->data << " "; if (cur->right) { stack.push(cur->right); } if (cur->left) { stack.push(cur->left); } } }
|
中序非递归 — 记忆区别 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
void InOrder(BTNode *root) { if(!root) exit(1); stack<BTNode*> stack; BTNode *cur = root; while(!stack.empty() || cur) { while (cur) { stack.push(cur); cur = cur->left; } if (!stack.empty()) { cur = stack.top(); stack.pop(); cout << cur->data << " "; cur = cur->right; } } }
|
后序非递归
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
void PostOrder(BTNode *root) { if(!root) exit(1); stack<BTNode*> stack1; stack<BTNode*> stack2; BTNode *cur; stack1.push(root); while(!stack1.empty()) { cur = stack1.top(); stack1.pop(); stack2.push(cur); if (cur->left) { stack1.push(cur->left); } if (cur->right) { stack1.push(right); } } while (!stack2.empty()) { cur = stack2.top(); cout<< cur->data << " "; stack2.pop(); } }
|
层序遍历 可记录层数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| void LevelOrder(BTNode *root) { queue<BTNode*> que; BTNode *cur; que.push(root); while (!que.empty()) { int size = que.size(); while (!que.empty()) { cur = que.front(); que.pop(); if (cur->left) { que.push(cur->left); } if (cur->right) { que.push(cur->right); } } } }
|
中序遍历的二叉树线索化 — 记忆
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
void Inthread(BTNode *p, BTNode *&pre) { if (p) { Inthread(p->left, pre); if (p->left==NULL) { p->left = pre; p->ltag = 1; } if (pre!=NULL && pre->right==NULL) { pre->right = p; p->rtag = 1; } pre = p; Inthread(p->rchild, pre); } }
|
二叉搜索树
二叉搜索树基本操作 — 记忆
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
TreeNode* insertIntoBST(TreeNode* root, int val) { TreeNode *p = root, *pre; while(p) { pre = p; if(p->val>val) p = p->left; else if(p->val<val) p = p->right; else return 0; } TreeNode *r = new TreeNode(val); if(root) { if(pre->val > val) pre->left = r; else pre->right = r; } else root = r; return root; }
TreeNode* deleteNode(TreeNode* root, int key) { if(root==NULL)return NULL; if(root->val>key) { root->left = deleteNode(root->left,key); return root; } if(root->val<key) { root->right = deleteNode(root->right,key); return root; } if(root->left==NULL&&root->right==NULL)return NULL; if(root->left==NULL&&root->right!=NULL)return root->right; if(root->left!=NULL&&root->right==NULL)return root->left; int val = findMax(root->left); root->val=val; root->left = deleteNode(root->left,val); return root; } int findMax(TreeNode* root) { if(root->right==NULL)return root->val; return findMax(root->right); }
|
二叉树的路径最大和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| int maxs = INT_MIN; int maxPathSum(TreeNode* root) { findroot(root); return maxs; } int findroot(TreeNode *root) { if(!root) return 0; int left = max( findroot(root->left), 0); int right = max( findroot(root->right), 0); int lmr = root->val + right + left; maxs = max(maxs, lmr); return max(0,root->val + max(left, right)); }
|
知识点和代码均学习于Acwing: https://www.acwing.com/activity/