第六章 二叉树part07
第六章 二叉树part07
235. 二叉搜索树的最近公共祖先 701.二叉搜索树中的插入操作
450.删除二叉搜索树中的节点
235. 二叉搜索树的最近公共祖先
题目地址 :
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++ )
{
