第六章 二叉树part08
第六章 二叉树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 ; // 二叉搜索树 内 的 结点 值 均在 左边界 范围 内