第六章 二叉树part06
530.二叉搜索树的最小绝对差 501.二叉搜索树中的众数 236. 二叉树的最近公共祖先
530.二叉搜索树的最小绝对差
题目链接 :
530. 二叉搜索树的最小绝对差 - 力扣(LeetCode)
Code : ( Basic 解 )
/**
* 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:
int getMinimumDifference(TreeNode* root) {
// 指针 的 分配
// 多指针 的 使用
// previous pointer 的 暂存
// 支持 结点 的 预处理
// 左 移 更新 左 relaive edge 值
// 由 子 结点 驱动 计算
// 历史 指针 的 使用
// TreeNode* node_LeftEdge ;
// TreeNode* node_RightEdge ;
// TreeNode* node_Work ;
// node_LeftEdge = root ;
// node_RightEdge = root ;
// node_Work = root ;
// 注意 递归 程序 的 规律
// 新 建 递归 程序 / 函数 , 再调用
int valueInt_MinDiff_For_Return ;
valueInt_MinDiff_For_Return = fuction_Recursion_Work(root , nullptr , nullptr) ;
return valueInt_MinDiff_For_Return ;
}
int fuction_Recursion_Work(TreeNode* node_In , TreeNode* node_LeftLongEdge_In ,TreeNode* node_RightLongEdge_In )
{
TreeNode* node_LeftLongEdge ;
TreeNode* node_RightLongEdge ;
TreeNode* node_Work ;
node_LeftLongEdge = node_LeftLongEdge_In ;
node_RightLongEdge = node_RightLongEdge_In ;
node_Work = node_In ;
// int diff_NodeThis_With_Left ;
// int diff_NodeThis_With_Right ;
// basic + 优化
// if(node_Work->left == nullptr)
// {
// diff_NodeThis_With_Left = -1 ;
// }
// if(node_Work->right == nullptr)
// {
// diff_NodeThis_With_Right = -1 ;
// }
// 状态 信息 与 值
// Conbination 的 情况
int diff_LeftChild = -1 ;
int diff_RightChild = -1 ;
// 左 子 结点 的 diff 的 更新
if(node_Work->left != nullptr)
{
//think
cout<< "node_Work->left->val : " << node_Work->left->val <<endl;
int diff_LeftChild_Right = abs(node_Work->val - node_Work->left->val );
if(node_LeftLongEdge != nullptr)
{
int diff_LeftChild_Left = abs(node_LeftLongEdge->val - node_Work->left->val );
diff_LeftChild = diff_LeftChild_Left < diff_LeftChild_Right ? diff_LeftChild_Left : diff_LeftChild_Right ;
}
else
{
diff_LeftChild = diff_LeftChild_Right ;
}
// think
}
// 右 子 结点 的 diff 的 更新
if(node_Work->right != nullptr)
{
//think
cout<< "node_Work->right->val : " << node_Work->right->val <<endl;
int diff_RightChild_Left = abs(node_Work->val - node_Work->right->val );
if(node_RightLongEdge != nullptr)
{
int diff_RightChild_Right = abs(node_RightLongEdge->val - node_Work->right->val );
diff_RightChild = diff_RightChild_Left < diff_RightChild_Right ? diff_RightChild_Left : diff_RightChild_Right ;
}
else
{
diff_RightChild = diff_RightChild_Left ;
}
// think
}
int diff_Send_To_UpperFloor ;
if(diff_LeftChild == -1 && diff_RightChild == -1 )
{
diff_Send_To_UpperFloor = -1 ;
}
else if(diff_LeftChild != -1 && diff_RightChild == -1 )
{
diff_Send_To_UpperFloor = diff_LeftChild ;
}
else if(diff_LeftChild == -1 && diff_RightChild != -1 )
{
diff_Send_To_UpperFloor = diff_RightChild ;
}
else if(diff_LeftChild != -1 && diff_RightChild != -1 )
{
diff_Send_To_UpperFloor = diff_LeftChild < diff_RightChild ? diff_LeftChild : diff_RightChild ;
}
// 递归 中 的 程序 布置 , 非 绝对 的 前序 , 中序 , 后序
int valueInt_Return_From_LeftChild ;
int valueInt_Return_From_RightChild ;
if(node_Work->left != nullptr )
{
// think
// 代码 现式 逻辑 优化
//node_RightLongEdge = node_Work ;
valueInt_Return_From_LeftChild = fuction_Recursion_Work(node_Work->left , node_LeftLongEdge ,node_Work) ;
}
if(node_Work->right != nullptr )
{
//node_LeftLongEdge = node_Work ; // if 在 此 递归 中 保留 node_LeftLongEdge 的 值
valueInt_Return_From_RightChild = fuction_Recursion_Work(node_Work->right , node_Work , node_RightLongEdge ) ;
}
// 对 子 层 返回 值 的 处理
if(diff_Send_To_UpperFloor == -1 )
{
if(valueInt_Return_From_LeftChild == -1 && valueInt_Return_From_RightChild == -1 )
{
diff_Send_To_UpperFloor = -1 ;
}
else if(valueInt_Return_From_LeftChild != -1 && valueInt_Return_From_RightChild == -1 )
{
diff_Send_To_UpperFloor = valueInt_Return_From_LeftChild ;
}
else if(valueInt_Return_From_LeftChild == -1 && valueInt_Return_From_RightChild != -1 )
{
diff_Send_To_UpperFloor = valueInt_Return_From_RightChild ;
}
else if(valueInt_Return_From_LeftChild != -1 && valueInt_Return_From_RightChild != -1 )
{
diff_Send_To_UpperFloor = valueInt_Return_From_LeftChild < valueInt_Return_From_RightChild ? valueInt_Return_From_LeftChild : valueInt_Return_From_RightChild ;
}
}
else
{
if(valueInt_Return_From_LeftChild == -1 && valueInt_Return_From_RightChild == -1 )
{
diff_Send_To_UpperFloor = diff_Send_To_UpperFloor ;
}
else if(valueInt_Return_From_LeftChild != -1 && valueInt_Return_From_RightChild == -1 )
{
diff_Send_To_UpperFloor = valueInt_Return_From_LeftChild < diff_Send_To_UpperFloor ? valueInt_Return_From_LeftChild : diff_Send_To_UpperFloor ;
}
else if(valueInt_Return_From_LeftChild == -1 && valueInt_Return_From_RightChild != -1 )
{
diff_Send_To_UpperFloor = valueInt_Return_From_RightChild < diff_Send_To_UpperFloor ? valueInt_Return_From_RightChild : diff_Send_To_UpperFloor ;
}
else if(valueInt_Return_From_LeftChild != -1 && valueInt_Return_From_RightChild != -1 )
{
diff_Send_To_UpperFloor = valueInt_Return_From_LeftChild < diff_Send_To_UpperFloor ? valueInt_Return_From_LeftChild : diff_Send_To_UpperFloor ;
diff_Send_To_UpperFloor = valueInt_Return_From_RightChild < diff_Send_To_UpperFloor ? valueInt_Return_From_RightChild : diff_Send_To_UpperFloor ;
}
}
return diff_Send_To_UpperFloor ;
}
};
Code : ( 使用 Infinity 数 优化 版 )
“ Different ”
/**
* 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:
int getMinimumDifference(TreeNode* root) {
// 指针 的 分配
// 多指针 的 使用
// previous pointer 的 暂存
// 支持 结点 的 预处理
// 左 移 更新 左 relaive edge 值
// 由 子 结点 驱动 计算
// 历史 指针 的 使用
// TreeNode* node_LeftEdge ;
// TreeNode* node_RightEdge ;
// TreeNode* node_Work ;
// node_LeftEdge = root ;
// node_RightEdge = root ;
// node_Work = root ;
// 注意 递归 程序 的 规律
// 新 建 递归 程序 / 函数 , 再调用
int valueInt_MinDiff_For_Return ;
valueInt_MinDiff_For_Return = fuction_Recursion_Work(root , nullptr , nullptr) ;
return valueInt_MinDiff_For_Return ;
}
int fuction_Recursion_Work(TreeNode* node_In , TreeNode* node_LeftLongEdge_In ,TreeNode* node_RightLongEdge_In )
{
// 设置 Infinity 数 版
int num_For_Infinity = 100001;
TreeNode* node_LeftLongEdge ;
TreeNode* node_RightLongEdge ;
TreeNode* node_Work ;
node_LeftLongEdge = node_LeftLongEdge_In ;
node_RightLongEdge = node_RightLongEdge_In ;
node_Work = node_In ;
// int diff_NodeThis_With_Left ;
// int diff_NodeThis_With_Right ;
// basic + 优化
// if(node_Work->left == nullptr)
// {
// diff_NodeThis_With_Left = -1 ;
// }
// if(node_Work->right == nullptr)
// {
// diff_NodeThis_With_Right = -1 ;
// }
// 状态 信息 与 值
// Conbination 的 情况
int diff_LeftChild = num_For_Infinity ;
int diff_RightChild = num_For_Infinity ;
// 左 子 结点 的 diff 的 更新
if(node_Work->left != nullptr)
{
//think
//cout<< "node_Work->left->val : " << node_Work->left->val <<endl;
int diff_LeftChild_Right = abs(node_Work->val - node_Work->left->val );
if(node_LeftLongEdge != nullptr)
{
int diff_LeftChild_Left = abs(node_LeftLongEdge->val - node_Work->left->val );
diff_LeftChild = diff_LeftChild_Left < diff_LeftChild_Right ? diff_LeftChild_Left : diff_LeftChild_Right ;
}
else
{
diff_LeftChild = diff_LeftChild_Right ;
}
// think
}
// 右 子 结点 的 diff 的 更新
if(node_Work->right != nullptr)
{
//think
//cout<< "node_Work->right->val : " << node_Work->right->val <<endl;
int diff_RightChild_Left = abs(node_Work->val - node_Work->right->val );
if(node_RightLongEdge != nullptr)
{
int diff_RightChild_Right = abs(node_RightLongEdge->val - node_Work->right->val );
diff_RightChild = diff_RightChild_Left < diff_RightChild_Right ? diff_RightChild_Left : diff_RightChild_Right ;
}
else
{
diff_RightChild = diff_RightChild_Left ;
}
// think
}
int diff_Send_To_UpperFloor = num_For_Infinity ;
diff_Send_To_UpperFloor = diff_LeftChild < diff_RightChild ? diff_LeftChild : diff_RightChild ;
// 递归 中 的 程序 布置 , 非 绝对 的 前序 , 中序 , 后序
int valueInt_Return_From_LeftChild = num_For_Infinity ;
int valueInt_Return_From_RightChild = num_For_Infinity ;
if(node_Work->left != nullptr )
{
// think
// 代码 现式 逻辑 优化
//node_RightLongEdge = node_Work ;
valueInt_Return_From_LeftChild = fuction_Recursion_Work(node_Work->left , node_LeftLongEdge ,node_Work) ;
}
if(node_Work->right != nullptr )
{
//node_LeftLongEdge = node_Work ; // if 在 此 递归 中 保留 node_LeftLongEdge 的 值
valueInt_Return_From_RightChild = fuction_Recursion_Work(node_Work->right , node_Work , node_RightLongEdge ) ;
}
// 对 子 层 返回 值 的 处理
diff_Send_To_UpperFloor = valueInt_Return_From_LeftChild < diff_Send_To_UpperFloor ? valueInt_Return_From_LeftChild : diff_Send_To_UpperFloor ;
diff_Send_To_UpperFloor = valueInt_Return_From_RightChild < diff_Send_To_UpperFloor ? valueInt_Return_From_RightChild : diff_Send_To_UpperFloor ;
//cout<<"diff_Send_To_UpperFloor : " << diff_Send_To_UpperFloor << endl;
// 初始化 存在 问题 ?
return diff_Send_To_UpperFloor ;
}
};
236. 二叉树的最近公共祖先
题目地址 :
236. 二叉树的最近公共祖先 - 力扣(LeetCode)
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 ;
}
};