第六章 二叉树part08
669. 修剪二叉搜索树 108.将有序数组转换为二叉搜索树
538.把二叉搜索树转换为累加树
669. 修剪二叉搜索树
题目 链接 :
Code ( 非递归 分支 处理 + 状态码 的 使用 ( 处理 过程 状态码 + “ Result 情况 状态码 ” + 后端 处理 状态码 ) ) :
/**
* Definition for a binary tree node.
* 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) {}
* };
*/
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
// Think
// Need Think
if(root == nullptr )
{
return root ;
}
// 对 左 边界 的 处理 / 相关 处理
int val_root = root->val ;
TreeNode * node_Work_For_LeftSide = root ;
TreeNode * node_Pre_From_Work_For_LeftSide = nullptr ;
TreeNode * root_New_For_Return = root ;
// 历史 指针 可以 选择 性 的 使用
// xx 结点 内 的 信息 也要 Authentication
// 这里 是 xx 结点 / Target 结点 右 子 结点 的 情况
int state_code_Front_For_LeftSide = 0 ;
// 1 左 搜
// 2 右 搜
int state_code_Backward_For_LeftSide = 0 ;
//cout<<111111111<<endl;
if(low < val_root )
{
state_code_Front_For_LeftSide = 1 ;
while(1)
{
//cout<< " node_Work_For_LeftSide->val : " << node_Work_For_LeftSide->val<<endl;
//cout<<1111<<endl;
//cout<<111111111<<endl;
if( state_code_Front_For_LeftSide == 1 )
{
// Think
// Debug
// Think
//cout<<2222<<endl;
if( low < node_Work_For_LeftSide -> val )
{
node_Pre_From_Work_For_LeftSide = node_Work_For_LeftSide ;
if(node_Work_For_LeftSide -> left != nullptr && node_Work_For_LeftSide->val >= low )
{
node_Work_For_LeftSide = node_Work_For_LeftSide -> left ;
}
else
{
if(node_Work_For_LeftSide -> left == nullptr)
{
state_code_Backward_For_LeftSide = 3 ; // 二叉搜索树 内 的 结点 值 均在 左边界 范围 内
// 动态 处理 数据结构
//cout<<111111111<<endl;
break ;
}
else if(node_Work_For_LeftSide->val < low)
{
node_Pre_From_Work_For_LeftSide -> left = nullptr ;
break ;
}
}
}
else if( low == node_Work_For_LeftSide -> val )
{
//cout<< " node_Work_For_LeftSide->val : " << node_Work_For_LeftSide->val<<endl;
//cout<<2222<<endl;
// 结点 值 唯一 时 的 处理
// 结点 值 不 唯一 时 的 处理
// if 对 此 题
while( node_Work_For_LeftSide->left != nullptr && node_Work_For_LeftSide->left->val == node_Work_For_LeftSide->val )
{
node_Pre_From_Work_For_LeftSide = node_Work_For_LeftSide ;
node_Work_For_LeftSide = node_Work_For_LeftSide -> left ;
}
//cout<< " node_Work_For_LeftSide->val : " << node_Work_For_LeftSide->val<<endl;
state_code_Backward_For_LeftSide = 4 ; // 找到 了 “ ( 未来 的 ) 左 边界点 ”
node_Work_For_LeftSide -> left = nullptr ;
break ;
}
else if( low > node_Work_For_LeftSide -> val )
{
if(node_Work_For_LeftSide -> right != nullptr )
{
state_code_Front_For_LeftSide = 2 ;
continue ;
}
else
{
//右侧 无 “ 夹层 子节点 ”
state_code_Backward_For_LeftSide = 5 ;
node_Pre_From_Work_For_LeftSide -> left = nullptr ;
break ;
}
}
}
else if(state_code_Front_For_LeftSide == 2 )
{
if(node_Work_For_LeftSide -> right != nullptr && node_Work_For_LeftSide->val < low )
{
// 之前 的 Pre 结点 已经 找到 了 所 需 位置 , 故 开始 保留 其 所在 位置 的 地址 / or 交 由 其它 指针 保存 , 并 继续 前进 ( 这里 是 跟进 ) / 处理 + 跟进
//node_Pre_From_Work_For_LeftSide = node_Work_For_LeftSide
node_Work_For_LeftSide = node_Work_For_LeftSide -> right ;
}
else
{
//右侧 有 “ 夹层 子节点 ”
if(node_Work_For_LeftSide->val >= low )
{
node_Pre_From_Work_For_LeftSide -> left = node_Work_For_LeftSide ;
node_Pre_From_Work_For_LeftSide = node_Work_For_LeftSide ;
}
else if(node_Work_For_LeftSide->right == nullptr )
{
//右侧 无 “ “ 所需 ” 夹层 子节点 ”
break ;
}
if(node_Work_For_LeftSide -> left != nullptr )
{
state_code_Front_For_LeftSide = 3 ;
continue ;
}
else
{
state_code_Backward_For_LeftSide = 6 ;
break ;
}
}
// 易于 实现 的 方法
// 维护 数据结构 运行 效率 的 方法
// 计算机 处理 效率 / 效率 化 的 方法
}
else if(state_code_Front_For_LeftSide == 3 )
{
// 轻量化 设计
// Simple 化 设计
// 要 达到 标准
// 模块化 设计 支持
if( node_Work_For_LeftSide->left != nullptr && low < node_Work_For_LeftSide -> val )
{
node_Pre_From_Work_For_LeftSide = node_Work_For_LeftSide ;
node_Work_For_LeftSide = node_Work_For_LeftSide -> left ;
}
else if( low == node_Work_For_LeftSide -> val )
{
// 结点 值 唯一 时 的 处理
// 结点 值 不 唯一 时 的 处理
// if 对 此 题
while( node_Work_For_LeftSide->left != nullptr && node_Work_For_LeftSide->left->val == node_Work_For_LeftSide->val )
{
node_Pre_From_Work_For_LeftSide = node_Work_For_LeftSide ;
node_Work_For_LeftSide = node_Work_For_LeftSide -> left ;
}
state_code_Backward_For_LeftSide = 7 ; // 找到 了 “ ( 未来 的 ) 左 边界点 ”
node_Work_For_LeftSide -> left = nullptr ;
break ;
}
else if( low > node_Work_For_LeftSide -> val )
{
if(node_Work_For_LeftSide -> right != nullptr )
{
state_code_Front_For_LeftSide = 2 ;
continue ;
}
else
{
//右侧 无 “ “ 下 一 ” 夹层 子节点 ”
state_code_Backward_For_LeftSide = 9 ;
node_Pre_From_Work_For_LeftSide -> left ;
break ;
}
}
else if( node_Work_For_LeftSide -> left == nullptr )
{
state_code_Backward_For_LeftSide = 8 ; // 此 下降 阶段 的 结点 值 均在 左边界 范围 内
break ;
}
}
}
}
else if( low == val_root )
{
node_Work_For_LeftSide = root ;
while(node_Work_For_LeftSide->left != nullptr && node_Work_For_LeftSide->left->val == node_Work_For_LeftSide->val )
{
node_Work_For_LeftSide = node_Work_For_LeftSide -> left ;
}
node_Work_For_LeftSide -> left = nullptr ;
}
else if( low > val_root )
{
state_code_Front_For_LeftSide = 4 ;
while(1)
{
//cout<< "node_Work_For_LeftSide->val : " << node_Work_For_LeftSide->val << endl ;
if( state_code_Front_For_LeftSide == 4 )
{
if(node_Work_For_LeftSide -> right != nullptr && node_Work_For_LeftSide->val < low )
{
// 之前 的 Pre 结点 已经 找到 了 所 需 位置 , 故 开始 保留 其 所在 位置 的 地址 / or 交 由 其它 指针 保存 , 并 继续 前进 ( 这里 是 跟进 ) / 处理 + 跟进
//node_Pre_From_Work_For_LeftSide = node_Work_For_LeftSide
node_Work_For_LeftSide = node_Work_For_LeftSide -> right ;
}
else
{
// 更新 根 结点
if(node_Work_For_LeftSide->val >= low )
{
root_New_For_Return = node_Work_For_LeftSide ;
node_Pre_From_Work_For_LeftSide = node_Work_For_LeftSide ;
}
else if(node_Work_For_LeftSide->right == nullptr )
{
// 无 符合 要求 的 子节点
root_New_For_Return = nullptr ;
break ;
}
if(node_Work_For_LeftSide -> left != nullptr )
{
state_code_Front_For_LeftSide = 3 ;
continue ;
}
else
{
state_code_Backward_For_LeftSide = 6 ;
break ;
}
}
}
else if(state_code_Front_For_LeftSide == 2 )
{
if(node_Work_For_LeftSide -> right != nullptr && node_Work_For_LeftSide->val < low )
{
// 之前 的 Pre 结点 已经 找到 了 所 需 位置 , 故 开始 保留 其 所在 位置 的 地址 / or 交 由 其它 指针 保存 , 并 继续 前进 ( 这里 是 跟进 ) / 处理 + 跟进
//node_Pre_From_Work_For_LeftSide = node_Work_For_LeftSide
node_Work_For_LeftSide = node_Work_For_LeftSide -> right ;
}
else
{
//右侧 有 “ 夹层 子节点 ”
if(node_Work_For_LeftSide->val >= low )
{
node_Pre_From_Work_For_LeftSide -> left = node_Work_For_LeftSide ;
node_Pre_From_Work_For_LeftSide = node_Work_For_LeftSide ;
}
else if(node_Work_For_LeftSide->right == nullptr )
{
//右侧 无 “ “ 所需 ” 夹层 子节点 ”
break ;
}
if(node_Work_For_LeftSide -> left != nullptr )
{
state_code_Front_For_LeftSide = 3 ;
continue ;
}
else
{
state_code_Backward_For_LeftSide = 9 ;
break ;
}
}
// 易于 实现 的 方法
// 维护 数据结构 运行 效率 的 方法
// 计算机 处理 效率 / 效率 化 的 方法
}
else if(state_code_Front_For_LeftSide == 3 )
{
// 轻量化 设计
// Simple 化 设计
// 要 达到 标准
// 模块化 设计 支持
if( node_Work_For_LeftSide->left != nullptr && low < node_Work_For_LeftSide -> val )
{
node_Pre_From_Work_For_LeftSide = node_Work_For_LeftSide ;
node_Work_For_LeftSide = node_Work_For_LeftSide -> left ;
} // 注意 分支 处理 优先级 / 顺序
else if( low == node_Work_For_LeftSide -> val )
{
// 结点 值 唯一 时 的 处理
// 结点 值 不 唯一 时 的 处理
// if 对 此 题
while( node_Work_For_LeftSide->left != nullptr && node_Work_For_LeftSide->left->val == node_Work_For_LeftSide->val )
{
node_Pre_From_Work_For_LeftSide = node_Work_For_LeftSide ;
node_Work_For_LeftSide = node_Work_For_LeftSide -> left ;
}
state_code_Backward_For_LeftSide = 7 ; // 找到 了 “ ( 未来 的 ) 左 边界点 ”
// 7+
node_Work_For_LeftSide -> left = nullptr ;
break ;
}
else if( low > node_Work_For_LeftSide -> val )
{
if(node_Work_For_LeftSide -> right != nullptr )
{
state_code_Front_For_LeftSide = 2 ;
continue ;
}
else
{
//右侧 无 “ “ 下 一 ” 夹层 子节点 ”
state_code_Backward_For_LeftSide = 9 ;
// 9+
node_Pre_From_Work_For_LeftSide -> left = nullptr ;
break ;
}
}
else if( node_Work_For_LeftSide -> left == nullptr )
{
state_code_Backward_For_LeftSide = 8 ; // 此 下降 阶段 的 结点 值 均在 左边界 范围 内
// 8+
break ;
}
}
}
}
// 对 右 边界 的 处理 / 相关 处理
TreeNode * node_Work_For_RightSide = root ;
// if( root_New_For_Return != nullptr )
// {
// node_Work_For_RightSide = root_New_For_Return ;
// }
// 用 新 根 结点 ?
// 其它 的 地方 有 待 协调
TreeNode * node_Pre_From_Work_For_RightSide = nullptr ;
int state_code_Front_For_RightSide = 0 ;
// 1 右 搜
// 2 左 搜
int state_code_Backward_For_RightSide = 0 ;
if(high > val_root )
{
state_code_Front_For_RightSide = 1 ;
while(1)
{
//cout<< "node_Work_For_RightSide -> val : " << node_Work_For_RightSide -> val <<endl ;
if( state_code_Front_For_RightSide == 1 )
{
if( high > node_Work_For_RightSide -> val )
{
node_Pre_From_Work_For_RightSide = node_Work_For_RightSide ;
if(node_Work_For_RightSide -> right != nullptr && node_Work_For_RightSide -> val <= high )
{
node_Work_For_RightSide = node_Work_For_RightSide -> right ;
}
else
{
if(node_Work_For_RightSide -> right == nullptr)
{
state_code_Backward_For_RightSide = 3 ; // 二叉搜索树 内 的 结点 值 均在 左边界 范围 内
// 动态 处理 数据结构
break ;
}
else if(node_Work_For_RightSide -> val > high)
{
node_Pre_From_Work_For_RightSide -> right = nullptr ;
break ;
}
}
}
else if( high == node_Work_For_RightSide -> val )
{
// 结点 值 唯一 时 的 处理
// 结点 值 不 唯一 时 的 处理
// if 对 此 题
while( node_Work_For_RightSide->right != nullptr && node_Work_For_RightSide->right->val == node_Work_For_RightSide->val )
{
node_Pre_From_Work_For_RightSide = node_Work_For_RightSide ;
node_Work_For_RightSide = node_Work_For_RightSide -> right ;
}
state_code_Backward_For_RightSide = 4 ; // 找到 了 “ ( 未来 的 ) 左 边界点 ”
node_Work_For_RightSide -> right = nullptr ;
break ;
}
else if( high < node_Work_For_RightSide -> val )
{
if(node_Work_For_RightSide -> left != nullptr )
{
state_code_Front_For_RightSide = 2 ;
//cout<<444444444<<endl;
continue ;
}
else
{
//右侧 无 “ 夹层 子节点 ”
state_code_Backward_For_RightSide = 5 ;
node_Pre_From_Work_For_RightSide -> right = nullptr ;
break ;
}
}
}
else if(state_code_Front_For_RightSide == 2 )
{
if(node_Work_For_RightSide -> left != nullptr && node_Work_For_RightSide->val > high )
{
// 之前 的 Pre 结点 已经 找到 了 所 需 位置 , 故 开始 保留 其 所在 位置 的 地址 / or 交 由 其它 指针 保存 , 并 继续 前进 ( 这里 是 跟进 ) / 处理 + 跟进
//node_Pre_From_Work_For_LeftSide = node_Work_For_LeftSide
//cout<<444444444<<endl;
node_Work_For_RightSide = node_Work_For_RightSide -> left ;
}
else
{
//右侧 有 “ 夹层 子节点 ”
if(node_Work_For_RightSide->val <= high )
{
node_Pre_From_Work_For_RightSide -> right = node_Work_For_RightSide ;
node_Pre_From_Work_For_RightSide = node_Work_For_RightSide ;
}
else if(node_Work_For_RightSide->left == nullptr )
{
//右侧 无 “ “ 所需 ” 夹层 子节点 ”
break ;
}
if(node_Work_For_RightSide -> right != nullptr )
{
state_code_Front_For_RightSide = 3 ;
continue ;
}
else
{
state_code_Backward_For_RightSide = 6 ;
break ;
}
}
// 易于 实现 的 方法
// 维护 数据结构 运行 效率 的 方法
// 计算机 处理 效率 / 效率 化 的 方法
}
else if(state_code_Front_For_RightSide == 3 )
{
// 轻量化 设计
// Simple 化 设计
// 要 达到 标准
// 模块化 设计 支持
//cout<<5555555<<endl;
if( node_Work_For_RightSide->right != nullptr && high > node_Work_For_RightSide -> val )
{
node_Pre_From_Work_For_RightSide = node_Work_For_RightSide ;
node_Work_For_RightSide = node_Work_For_RightSide -> right ;
}
else if( high == node_Work_For_RightSide -> val )
{
// 结点 值 唯一 时 的 处理
// 结点 值 不 唯一 时 的 处理
// if 对 此 题
while( node_Work_For_RightSide->right != nullptr && node_Work_For_RightSide->right->val == node_Work_For_RightSide->val )
{
node_Pre_From_Work_For_RightSide = node_Work_For_RightSide ;
node_Work_For_RightSide = node_Work_For_RightSide -> right ;
}
state_code_Backward_For_RightSide = 7 ; // 找到 了 “ ( 未来 的 ) 左 边界点 ”
node_Work_For_RightSide -> right = nullptr ;
break ;
}
else if( high < node_Work_For_RightSide -> val )
{
if(node_Work_For_RightSide -> left != nullptr )
{
state_code_Front_For_RightSide = 2 ;
continue ;
}
else
{
//右侧 无 “ “ 下 一 ” 夹层 子节点 ”
state_code_Backward_For_RightSide = 9 ;
}
}
else if( node_Work_For_RightSide -> right == nullptr )
{
state_code_Backward_For_RightSide = 8 ; // 此 下降 阶段 的 结点 值 均在 左边界 范围 内
break ;
}
}
}
}
else if( high == val_root )
{
node_Work_For_RightSide = root ;
while(node_Work_For_RightSide->right != nullptr && node_Work_For_RightSide->right->val == node_Work_For_RightSide->val )
{
node_Work_For_RightSide = node_Work_For_RightSide -> right ;
}
node_Work_For_RightSide -> right = nullptr ;
}
else if( high < val_root )
{
state_code_Front_For_RightSide = 4 ;
while(1)
{
//cout<<111111111<<endl;
if( state_code_Front_For_RightSide == 4 )
{
if(node_Work_For_RightSide -> left != nullptr && node_Work_For_RightSide->val > high )
{
// 之前 的 Pre 结点 已经 找到 了 所 需 位置 , 故 开始 保留 其 所在 位置 的 地址 / or 交 由 其它 指针 保存 , 并 继续 前进 ( 这里 是 跟进 ) / 处理 + 跟进
//node_Pre_From_Work_For_LeftSide = node_Work_For_LeftSide
node_Work_For_RightSide = node_Work_For_RightSide -> left ;
}
else
{
// 更新 根 结点
if(node_Work_For_RightSide->val <= high )
{
root_New_For_Return = node_Work_For_RightSide ;
node_Pre_From_Work_For_RightSide = node_Work_For_RightSide ;
}
else if(node_Work_For_RightSide->left == nullptr )
{
// 无 符合 要求 的 子节点
root_New_For_Return = nullptr ;
break ;
}
if(node_Work_For_RightSide -> right != nullptr )
{
state_code_Front_For_RightSide = 3 ;
continue ;
}
else
{
state_code_Backward_For_RightSide = 6 ;
break ;
}
}
}
else if(state_code_Front_For_RightSide == 2 )
{
if(node_Work_For_RightSide -> left != nullptr && node_Work_For_RightSide->val > high )
{
// 之前 的 Pre 结点 已经 找到 了 所 需 位置 , 故 开始 保留 其 所在 位置 的 地址 / or 交 由 其它 指针 保存 , 并 继续 前进 ( 这里 是 跟进 ) / 处理 + 跟进
//node_Pre_From_Work_For_LeftSide = node_Work_For_LeftSide
node_Work_For_RightSide = node_Work_For_RightSide -> left ;
}
else
{
//右侧 有 “ 夹层 子节点 ”
if(node_Work_For_RightSide->val <= high )
{
node_Pre_From_Work_For_RightSide -> right = node_Work_For_RightSide ;
node_Pre_From_Work_For_RightSide = node_Work_For_RightSide ;
}
else if(node_Work_For_RightSide->left == nullptr )
{
//右侧 无 “ “ 所需 ” 夹层 子节点 ”
break ;
}
if(node_Work_For_RightSide -> right != nullptr )
{
state_code_Front_For_RightSide = 3 ;
continue ;
}
else
{
state_code_Backward_For_RightSide = 9 ;
break ;
}
}
// 易于 实现 的 方法
// 维护 数据结构 运行 效率 的 方法
// 计算机 处理 效率 / 效率 化 的 方法
}
else if(state_code_Front_For_RightSide == 3 )
{
// 轻量化 设计
// Simple 化 设计
// 要 达到 标准
// 模块化 设计 支持
//cout<< "node_Work_For_RightSide -> val : " <<node_Work_For_RightSide -> val<<endl;
if( node_Work_For_RightSide->right != nullptr && high > node_Work_For_RightSide -> val )
{
node_Pre_From_Work_For_RightSide = node_Work_For_RightSide ;
node_Work_For_RightSide = node_Work_For_RightSide -> right ;
}
else if( high == node_Work_For_RightSide -> val )
{
// 结点 值 唯一 时 的 处理
// 结点 值 不 唯一 时 的 处理
// if 对 此 题
while( node_Work_For_RightSide->right != nullptr && node_Work_For_RightSide->right->val == node_Work_For_RightSide->val )
{
node_Pre_From_Work_For_RightSide = node_Work_For_RightSide ;
node_Work_For_RightSide = node_Work_For_RightSide -> right ;
}
state_code_Backward_For_RightSide = 7 ; // 找到 了 “ ( 未来 的 ) 左 边界点 ”
// 7+
node_Work_For_RightSide -> right = nullptr ;
break ;
}
else if( high < node_Work_For_RightSide -> val )
{
//cout<<33333333333<<endl;
if(node_Work_For_RightSide -> left != nullptr )
{
//cout<<33333333333<<endl;
state_code_Front_For_RightSide = 2 ;
continue ;
}
else
{
//cout<<33333333333<<endl;
//右侧 无 “ “ 下 一 ” 夹层 子节点 ”
state_code_Backward_For_RightSide = 9 ;
// 9+
node_Pre_From_Work_For_RightSide -> right = nullptr ;
break ;
}
}
else if( node_Work_For_RightSide -> right == nullptr )
{
//cout<<33333333333<<endl;
state_code_Backward_For_RightSide = 8 ; // 此 下降 阶段 的 结点 值 均在 左边界 范围 内
// 8+
break ;
}
}
}
}
// 设置 有效 的 根节点
if( root_New_For_Return == nullptr )
{
}
else
{
while(root_New_For_Return ->val < low || root_New_For_Return -> val > high )
{
if(root_New_For_Return ->val < low)
{
if(root_New_For_Return -> right != nullptr )
{
root_New_For_Return = root_New_For_Return -> right ;
}
else
{
root_New_For_Return = nullptr ;
}
}
else if( root_New_For_Return -> val > high )
{
if(root_New_For_Return -> left != nullptr )
{
root_New_For_Return = root_New_For_Return -> left ;
}
else
{
root_New_For_Return = nullptr ;
}
}
}
}
return root_New_For_Return ;
}
};
108.将有序数组转换为二叉搜索树
题目链接 :
108. 将有序数组转换为二叉搜索树 - 力扣(LeetCode)
Code :
538.把二叉搜索树转换为累加树
题目链接 :
538. 把二叉搜索树转换为累加树 - 力扣(LeetCode)
Code :