第六章 二叉树part07

27 阅读5分钟

第六章 二叉树part07

235. 二叉搜索树的最近公共祖先 701.二叉搜索树中的插入操作

450.删除二叉搜索树中的节点

235. 二叉搜索树的最近公共祖先

题目地址 :

235. 二叉搜索树的最近公共祖先 - 力扣(LeetCode)

Code : ( 之前 “二叉树的最近公共祖先 ” 的 Code )

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    struct Node_And_StateCodeInside
    {
        TreeNode * node = NULL ;
        int state_Code_Inside = 0 ;
​
    };
​
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        
        
        // 路径 记录 
​
        // 利用 参考 长度 信息  
​
        // 共同 祖先 特点 , 之 一  : 前 一段 是  相等  
​
​
        // 链表 分支  
​
        // First 下一个 结点 不同 的 结点  / 一方  长度  不足 / 结束 时  
​
​
        // 路径 的 记录  与  维护  
​
​
        // 信息 流  的  合理 处理    
​
​
​
        // 递归 函数 可以 单独 放置 处理  
​
​
        // 这里 利用 后续 处理  ?  
​
​
​
        // 回溯 法    
​
​
​
        TreeNode* node_Needle ;
​
​
        //while( node_Needle !=)
​
​
        vector<TreeNode * > vec_NodePath_Directly_p = find_NodePath_Directly(root , p) ;
​
        vector<TreeNode * > vec_NodePath_Directly_q = find_NodePath_Directly(root , q) ;
​
        int length_vec_NodePath_Directly_p = vec_NodePath_Directly_p.size() ;
        int length_vec_NodePath_Directly_q = vec_NodePath_Directly_q.size() ;
​
        int i = 0 ;
​
        if(length_vec_NodePath_Directly_p <= length_vec_NodePath_Directly_q)
        {
            for(  ; i < length_vec_NodePath_Directly_p ; i++ )
            {
                if(vec_NodePath_Directly_p[i] == vec_NodePath_Directly_q[i] )
                {
                    continue ;
                }
                else            
                {
                                // 下标 记录  /  need 下标 信息  
​
                    break ;  // “ break  ”
​
                }
​
​
​
            }
​
​
        }
        else if(length_vec_NodePath_Directly_q < length_vec_NodePath_Directly_p )
        {
            for(  ; i < length_vec_NodePath_Directly_q ; i++ )
            {
                if(vec_NodePath_Directly_p[i] == vec_NodePath_Directly_q[i] )
                {
                    continue ;
                }
                else            
                {
                                // 下标 记录  /  need 下标 信息  
​
                    break ;  // “ break  ”
​
                }
​
​
​
            }
​
​
        }
​
        return vec_NodePath_Directly_p[i - 1 ] ;
​
​
​
​
​
​
​
​
    }
​
​
​
    vector< TreeNode* > find_NodePath_Directly(TreeNode* root, TreeNode* node_Target)
    {
​
​
        TreeNode* node_Needle = root ;
                                            // xx 是  来   支持   的
                                            // Let ' s   go    
        
        
        stack<Node_And_StateCodeInside> stack_PerviousAncestorNode ;
​
​
        // 下降 阶段 
​
        // 上升 阶段  
​
​
        int state_Code = 0 ;
​
​
        state_Code = 1 ;
​
​
​
        //while( node_Needle !- node_Target )
        while( 1 )
        {
            //cout<<111111111111111<<endl;
            //cout<<"node_Needle->val : "<<node_Needle->val<<endl;
            if(state_Code == 1 && ( (stack_PerviousAncestorNode.empty() != 1 && node_Needle != stack_PerviousAncestorNode.top().node) || stack_PerviousAncestorNode.empty() == 1 ) )            // 下降 阶段  
            {
                if(node_Needle == node_Target)
                {
                    Node_And_StateCodeInside node_And_StateCodeInside_Temp;
                    node_And_StateCodeInside_Temp.node = node_Needle ;
                    node_And_StateCodeInside_Temp.state_Code_Inside = 0 ;
​
                    stack_PerviousAncestorNode.push(node_And_StateCodeInside_Temp) ;
​
                    state_Code = 3 ;
​
                    break ;
                }
                else if(node_Needle != node_Target)         // 代码 可读性 的 增强    
                {
                    Node_And_StateCodeInside node_And_StateCodeInside_Temp;
                    node_And_StateCodeInside_Temp.node = node_Needle ;
                    node_And_StateCodeInside_Temp.state_Code_Inside = 1 ;
​
                    stack_PerviousAncestorNode.push(node_And_StateCodeInside_Temp) ;
​
​
                    if(node_Needle->left != NULL )
                    {
​
                        node_Needle = node_Needle->left ;
​
                    }
                    else if(node_Needle->left == NULL)
                    {
​
​
​
                    }
​
​
                    // 递归 过程 的 模拟  
​
                    
​
                }
​
                // Different Ways  
​
​
​
​
                // 实在 不行 可以 跟进 的 存 状态码 信息 , 可以 是 multi 的 , 可以 使用 结构体  
​
            }
            else if(state_Code == 1 && stack_PerviousAncestorNode.empty() != 1 && node_Needle == stack_PerviousAncestorNode.top().node)
            {
                if(stack_PerviousAncestorNode.top().state_Code_Inside == 1 ) // 开始 向 右 子 结点   搜
                {
                    stack_PerviousAncestorNode.top().state_Code_Inside = 2 ;
​
                    if(stack_PerviousAncestorNode.top().node->right != NULL )
                    {
                        node_Needle = stack_PerviousAncestorNode.top().node->right ;
​
                    }
                    else
                    {
                        stack_PerviousAncestorNode.pop();
​
                        if(stack_PerviousAncestorNode.empty() != 1 )
                        {
                            node_Needle = stack_PerviousAncestorNode.top().node;
                        }
                        else
                        {
                            state_Code = 4 ;
                        }
                        
​
                    }
​
​
                }
                else if(stack_PerviousAncestorNode.top().state_Code_Inside == 2 )
                {
​
                    stack_PerviousAncestorNode.pop();
​
                    if(stack_PerviousAncestorNode.empty() != 1 )
                    {
                        node_Needle = stack_PerviousAncestorNode.top().node;
                    }
                    else
                    {
                        state_Code = 4 ;
                    }
                    
​
​
                }
​
            }
            else if(state_Code == 2 )
            {
​
​
            }
            else if(state_Code == 3 )
            {
​
​
​
            }
​
​
​
​
        }
​
​
        stack<Node_And_StateCodeInside> stack_Cache_Temp;
​
        while(stack_PerviousAncestorNode.empty() != 1 )
        {
            stack_Cache_Temp.push(stack_PerviousAncestorNode.top());
            
            stack_PerviousAncestorNode.pop();
​
        }
​
        vector<TreeNode * > vec_TreeNode_For_Return ;
​
        while(stack_Cache_Temp.empty() != 1 )
        {
            vec_TreeNode_For_Return.push_back(stack_Cache_Temp.top().node) ;
​
            stack_Cache_Temp.pop() ;
​
            
        }
​
​
        //cout<<111111111111111<<endl;
​
​
​
        return vec_TreeNode_For_Return ;
​
​
​
​
​
    }
​
​
​
};

依据 数据结构 的 有序 性 优化 搜索 Logic ( 效率 ,/ 逻辑性 / “ 可读 性 ” / 人 可 理解 性 / 人 易 理解 性 / Simple 化 / 通用 性 )

// 通用 化 (在 xx 情况 )

// 对 适用 情况 的 分析

Code :

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
​
        // 数据 的 有序 性  特征            
​
​
​
        //  实际 编程 中  xx  题目  并非  不 可  解    /    那么 难  解    
​
​
        // when really action 时 
​
        // when 情况 真的 迫使 时    
​
        // will be different 
        
​
​
        // 此 题 
​
        // 结点 的 搜索 变 简单 了 
​
​
        vector<TreeNode * > vec_PathNode_To_p = find_PathNode_To_XXNode(root , p) ;
​
        vector<TreeNode * > vec_PathNode_To_q = find_PathNode_To_XXNode(root , q) ;
​
​
        int length_vec_PathNode_To_p = vec_PathNode_To_p.size() ;
        int length_vec_PathNode_To_q = vec_PathNode_To_q.size() ;
​
​
        int i = 0 ;
​
        if(length_vec_PathNode_To_p <= length_vec_PathNode_To_q )
        {
            for( ; i < length_vec_PathNode_To_p ; i++ )
            {
                if(vec_PathNode_To_p[i] == vec_PathNode_To_q[i] )
                {
​
                    continue ;
​
​
                }
                else
                {
                    break ;
​
​
                }
​
​
            }
​
​
        }
        else if(length_vec_PathNode_To_q < length_vec_PathNode_To_p )
        {
​
            for( ; i < length_vec_PathNode_To_q ; i++ )
            {
                if(vec_PathNode_To_p[i] == vec_PathNode_To_q[i] )
                {
​
                    continue ;
​
​
                }
                else
                {
                    break ;
​
​
                }
​
​
            }
​
​
​
        }
​
​
​
​
        return vec_PathNode_To_p[i - 1 ] ;
​
        
​
​
​
​
​
​
​
​
    }
​
​
    vector<TreeNode * > find_PathNode_To_XXNode(TreeNode* root, TreeNode* node_Target)
    {
​
        TreeNode * node_Work_This = root  ;
​
​
        vector< TreeNode * > vec_TreeNode_For_Return ;
​
​
        //while( node_Work_This != node_Target )
        while( 1 )
        {
            if( node_Work_This != node_Target )
            {
                vec_TreeNode_For_Return.push_back(node_Work_This) ;
​
                if(node_Target->val <= node_Work_This->val )
                {
                    if(node_Work_This->left != NULL)
                    {
                        node_Work_This = node_Work_This->left ;
                    }
                    else
                    {
                        cout<<" Error : node_Work_This->left == NULL "<<endl ;
​
                    }
​
                }
                else if( node_Target->val > node_Work_This->val )
                {
                    if(node_Work_This->right != NULL)
                    {
                        node_Work_This = node_Work_This->right ;
                    }
                    else
                    {
                        cout<<" Error : node_Work_This->right == NULL "<<endl ;
​
                    }
​
​
                }
            }
            else
            {
                vec_TreeNode_For_Return.push_back(node_Work_This) ;
​
                break ; 
​
            }
            
​
​
        }
​
​
​
​
        return vec_TreeNode_For_Return ;
​
​
​
        
​
​
​
​
​
    }
​
​
};

701.二叉搜索树中的插入操作

题目链接 :

701. 二叉搜索树中的插入操作 - 力扣(LeetCode)

Code :

/**
 * 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* insertIntoBST(TreeNode* root, int val) {
​
​
        TreeNode* node_Work_This = root ;
​
​
        if(root == nullptr )
        {
​
            TreeNode * node_Ptr_Temp_For_Construct = new TreeNode(val , nullptr , nullptr ) ;
​
            return  node_Ptr_Temp_For_Construct ;
​
​
        }
​
​
        while(1)
        {
            //if(node_Work_This == )
​
​
            // cout<<111111111111111<<endl;
​
            // if(node_Work_This == nullptr)
            // {
            //     cout<<2222222222<<endl;
​
            // }
            
​
​
            if( val <= node_Work_This->val )
            {
                if(node_Work_This->left != nullptr )
                {
                    node_Work_This = node_Work_This->left ;
​
                }
                else
                {   
                    // TreeNode node_Temp_For_Construct =  TreeNode(val , nullptr , nullptr ) ;
​
                    // TreeNode * node_Ptr_Temp_For_Construct = &node_Temp_For_Construct ;
​
                    TreeNode * node_Ptr_Temp_For_Construct = new TreeNode(val , nullptr , nullptr ) ;
​
                    node_Work_This->left = node_Ptr_Temp_For_Construct ;
​
                    break ;
​
​
                }
​
​
            }
            else if( val > node_Work_This->val )
            {
                if(node_Work_This->right != nullptr )
                {
                    node_Work_This = node_Work_This->right ;
​
                }
                else
                {   
​
                    // 注意 C++ 动态 分配 的 语法  
​
                    TreeNode * node_Ptr_Temp_For_Construct = new TreeNode(val , nullptr , nullptr ) ;
​
                    // TreeNode node_Temp_For_Construct =  TreeNode(val , nullptr , nullptr ) ;
​
                    // TreeNode * node_Ptr_Temp_For_Construct = &node_Temp_For_Construct ;
​
                    node_Work_This->right = node_Ptr_Temp_For_Construct ;
​
                    break ;
​
​
                }
                
​
​
​
            }
​
​
​
​
        }
​
​
        return root ;
​
​
​
​
    }
​
​
};

Code ( 文本 缩减 版 ) :

/**
 * 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* insertIntoBST(TreeNode* root, int val) {
        TreeNode* node_Work_This = root ;
        if(root == nullptr )
        {
            TreeNode * node_Ptr_Temp_For_Construct = new TreeNode(val , nullptr , nullptr ) ;
            return  node_Ptr_Temp_For_Construct ;
        }
        while(1)
        {
            if( val <= node_Work_This->val )
            {
                if(node_Work_This->left != nullptr )
                {
                    node_Work_This = node_Work_This->left ;
                }
                else
                {   
                    TreeNode * node_Ptr_Temp_For_Construct = new TreeNode(val , nullptr , nullptr ) ;
                    node_Work_This->left = node_Ptr_Temp_For_Construct ;
                    break ;
                }
            }
            else if( val > node_Work_This->val )
            {
                if(node_Work_This->right != nullptr )
                {
                    node_Work_This = node_Work_This->right ;
                }
                else
                {   
                    // 注意 C++ 动态 分配 的 语法  
                    TreeNode * node_Ptr_Temp_For_Construct = new TreeNode(val , nullptr , nullptr ) ;
                    node_Work_This->right = node_Ptr_Temp_For_Construct ;
                    break ;
                }
            }
        }
        return root ;
    }
};