如有错误请指出
从根节点上方开始从左子树方向开始画流水线,顺着画,返回到根节点上方
先序遍历:流水线依次到达 节点左侧 的顺序 :532146
中序遍历:流水线依次到达 节点下边 的顺序 :123456
后序遍历:流水线依次到达 节点右侧 的顺序 :124365
递归方法
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode():val(0),left(nullptr),right(nullptr){}
TreeNode(int x):val(x),left(nullptr),right(nullptr){}
TreeNode(int x,TreeNode* left,TreeNode* right):val(x),left(left),right(right){}
};
//前序遍历
void preorder(TreeNode *root, vector<int> &res)
{
if (root == nullptr)
{
return;
}
res.push_back(root->val);
preorder(root->left, res);
preorder(root->right, res);
}
vector<int> preorderTraversal(TreeNode *root)
{
vector<int> res;
preorder(root, res);
return res;
}
//中序遍历
void inorder(TreeNode* root,vector<int> &res)
{
if(root == nullptr)
{
return;
}
inorder(root->left,res);
res.push_back(root->val);
inorder(root->right,res);
}
vector<int> inorderTraversal(TreeNode *root)
{
vector<int> res;
inorder(root, res);
return res;
}
//后序遍历
void postorder(TreeNode* root,vector<int> &res)
{
if(root == nullptr)
{
return;
}
postorder(root->left,res);
postorder(root->right,res);
res.push_back(root->val);
}
vector<int> postorderTraversal(TreeNode *root)
{
vector<int> res;
postorder(root, res);
return res;
}
非递归方法
//非递归前序遍历
void preOrder(TreeNode *root)
{
stack<TreeNode*> s;
TreeNode *p=root;
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 inOrder(TreeNode *root)
{
stack<TreeNode*> s;
TreeNode *p=root;
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 postOrder(TreeNode *root)
{
stack<TreeNode*> s;
TreeNode *cur; //当前结点
TreeNode *pre=NULL; //前一次访问的结点
s.push(root);
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);
}
}
}
力扣——剑指OfferⅡ53:二叉搜索树的中后续遍历
官方答案方法一:中序遍历
为了找到二叉搜索树中的节点 p 的中序后继,最直观的方法是中序遍历。由于只需要找到节点 p 的中序后继,因此不需要维护完整的中序遍历序列,只需要在中序遍历的过程中维护上一个访问的节点和当前访问的节点。如果上一个访问的节点是节点 p,则当前访问的节点即为节点 p 的中序后继。
如果节点 p 是最后被访问的节点,则不存在节点 pp 的中序后继,返回 null。
class Solution {
public:
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p)
{
stack<TreeNode*> st;
TreeNode *prev = nullptr, *curr = root;
while (!st.empty() || curr != nullptr)
{
while (curr != nullptr)
{
st.emplace(curr);
curr = curr->left;
}
curr = st.top();
st.pop();
if (prev == p)
{
return curr;
}
prev = curr;
curr = curr->right;
}
return nullptr;
}
};
方法二:利用二叉搜素树的性质
二叉搜索树的一个性质是中序遍历序列单调递增,因此二叉搜索树中的节点 p 的中序后继满足以下条件:
中序后继的节点值大于 p 的节点值;
中序后继是节点值大于 p 的节点值的所有节点中节点值最小的一个节点。
利用二叉搜索树的性质,可以在不做中序遍历的情况下找到节点 p 的中序后继。
如果节点 p 的右子树不为空,则节点 p 的中序后继在其右子树中,在其右子树中定位到最左边的节点,即为节点 p 的中序后继。
如果节点 p 的右子树为空,则需要从根节点开始遍历寻找节点 p 的祖先节点。
将答案初始化为null。用 node 表示遍历到的节点,初始时 node=root。每次比较 node 的节点值和 p 的节点值,执行相应操作:
如果 \textit{node}node 的节点值大于 p 的节点值,则 p 的中序后继可能是 node 或者在 node 的左子树中,因此用node 更新答案,并将 node 移动到其左子节点继续遍历;
如果 node 的节点值小于或等于 p 的节点值,则 pp 的中序后继可能在 node 的右子树中,因此将 node 移动到其右子节点继续遍历。
由于在遍历过程中,当且仅当 node 的节点值大于 p 的节点值的情况下,才会用node 更新答案,因此当节点 p 有中序后继时一定可以找到中序后继,当节点 p 没有中序后继时答案一定为 null
class Solution
{
public:
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p)
{
TreeNode *successor = nullptr;
if (p->right != nullptr)
{
successor = p->right;
while (successor->left != nullptr)
{
successor = successor->left;
}
return successor;
}
TreeNode *node = root;
while (node != nullptr)
{
if (node->val > p->val)
{
successor = node;
node = node->left;
} else
{
node = node->right;
}
}
return successor;
}
};
力扣:剑指offerⅡ054:所有大于等于节点的值之和
方法一:反序中序遍历
class Solution {
public:
int sum = 0;
TreeNode* convertBST(TreeNode* root) {
if (root != nullptr) {
convertBST(root->right);
sum += root->val;
root->val = sum;
convertBST(root->left);
}
return root;
}
};
方法二:Morris 遍历
Morris 遍历的核心思想是利用树的大量空闲指针,实现空间开销的极限缩减。其反序中序遍历规则总结如下:
1.如果当前节点的右子节点为空,处理当前节点,并遍历当前节点的左子节点;
2.如果当前节点的右子节点不为空,找到当前节点右子树的最左节点(该节点为当前节点中序遍历的前驱节点);
2.1 如果最左节点的左指针为空,将最左节点的左指针指向当前节点,遍历当前节点的右子节点;
2.2如果最左节点的左指针不为空,将最左节点的左指针重新置为空(恢复树的原状),处理当前节点,并将当前节点置为其左节点;
3.重复步骤 1 和步骤 2,直到遍历结束。
class Solution {
public:
TreeNode* getSuccessor(TreeNode* node) {
TreeNode* succ = node->right;
while (succ->left != nullptr && succ->left != node) {
succ = succ->left;
}
return succ;
}
TreeNode* convertBST(TreeNode* root) {
int sum = 0;
TreeNode* node = root;
while (node != nullptr) {
if (node->right == nullptr) {
sum += node->val;
node->val = sum;
node = node->left;
} else {
TreeNode* succ = getSuccessor(node);
if (succ->left == nullptr) {
succ->left = node;
node = node->right;
} else {
succ->left = nullptr;
sum += node->val;
node->val = sum;
node = node->left;
}
}
}
return root;
}
};
题目代码链接:
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/P5rCT8/solution/er-cha-sou-suo-shu-zhong-de-zhong-xu-hou-5nrz/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/w6cpku/solution/suo-you-da-yu-deng-yu-jie-dian-de-zhi-zh-k02c/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。