日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Leetcode236 最近公共祖先-二叉树两次遍历

發(fā)布時(shí)間:2025/4/5 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Leetcode236 最近公共祖先-二叉树两次遍历 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目

給定一個(gè)二叉樹, 找到該樹中兩個(gè)指定節(jié)點(diǎn)的最近公共祖先。

百度百科中最近公共祖先的定義為:“對于有根樹 T 的兩個(gè)結(jié)點(diǎn) p、q,最近公共祖先表示為一個(gè)結(jié)點(diǎn) x,滿足 x 是 p、q 的祖先且 x 的深度盡可能大(一個(gè)節(jié)點(diǎn)也可以是它自己的祖先)。”

例如,給定如下二叉樹: root = [3,5,1,6,2,0,8,null,null,7,4]

示例 1:
輸入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
輸出: 3
解釋: 節(jié)點(diǎn) 5 和節(jié)點(diǎn) 1 的最近公共祖先是節(jié)點(diǎn) 3。
示例 2:
輸入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
輸出: 5
解釋: 節(jié)點(diǎn) 5 和節(jié)點(diǎn) 4 的最近公共祖先是節(jié)點(diǎn) 5。因?yàn)楦鶕?jù)定義最近公共祖先節(jié)點(diǎn)可以為節(jié)點(diǎn)本身。

說明:

所有節(jié)點(diǎn)的值都是唯一的。
p、q 為不同節(jié)點(diǎn)且均存在于給定的二叉樹中。

思路

首先,需要會(huì):返回二叉樹中根節(jié)點(diǎn)到任意節(jié)點(diǎn)的路徑
調(diào)用兩次該函數(shù),返回p節(jié)點(diǎn)和q節(jié)點(diǎn)的路徑。這里的根節(jié)點(diǎn)到任意節(jié)點(diǎn)的路徑代碼思路:筆者的另一篇博客

其次,遍歷兩個(gè)路徑數(shù)組。因?yàn)槎际菑母?jié)點(diǎn)開始的,所以順序遍歷到相同的值,則是公共祖先。

再次,最近公共祖先,是距離根節(jié)點(diǎn)最遠(yuǎn)的公共祖先,隨著循環(huán)變量i的增加,不斷更新公共祖先,這樣得到的就是最遠(yuǎn)的公共祖先。

leetcode提交

/*** 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) {vector<TreeNode*>path;//遍歷時(shí)候的臨時(shí)棧 vector<TreeNode*> node_p_path;//p結(jié)點(diǎn)路徑 vector<TreeNode*> node_q_path; //q結(jié)點(diǎn)路徑 int finish=0;pre_order(root,p,path,node_p_path,finish);//找到p節(jié)點(diǎn)路徑 ,存于node_p_path path.clear();//清空 finish=0; //清空 pre_order(root,q,path,node_q_path,finish);//找到q節(jié)點(diǎn)路徑,存于node_q_path int path_len=0;//較短路徑的長度if(node_p_path.size()<node_q_path.size())path_len=node_p_path.size();elsepath_len=node_q_path.size();TreeNode *result=0;for(int i=0;i<path_len;++i){if(node_p_path[i]==node_q_path[i])//相同節(jié)點(diǎn) result=node_p_path[i];//相同則更新,直到距離根節(jié)點(diǎn)最遠(yuǎn) }return result; }//前序遍歷,找到某一結(jié)點(diǎn)的路徑resultvoid pre_order(TreeNode * node,TreeNode *search,vector<TreeNode*> &path, vector<TreeNode*> &result,//返回路徑結(jié)果 int &finish){if(!node||finish==1)//為空或者找到則返回 return;path.push_back(node);//保存節(jié)點(diǎn)數(shù)值if(node==search){finish=1;result=path;//找到則節(jié)點(diǎn)路徑保存 } pre_order(node->left,search,path,result,finish);pre_order(node->right,search,path,result,finish);path.pop_back();//結(jié)束遍歷node時(shí),node節(jié)點(diǎn)彈出path棧 } };

測試代碼

#include<iostream>#include<vector>using namespace std;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){vector<TreeNode*>path;//遍歷時(shí)候的臨時(shí)棧 vector<TreeNode*> node_p_path;//p結(jié)點(diǎn)路徑 vector<TreeNode*> node_q_path; //q結(jié)點(diǎn)路徑 int finish=0;pre_order(root,p,path,node_p_path,finish);//找到p節(jié)點(diǎn)路徑 ,存于node_p_path path.clear();//清空 finish=0; //清空 pre_order(root,q,path,node_q_path,finish);//找到q節(jié)點(diǎn)路徑,存于node_q_path int path_len=0;//較短路徑的長度if(node_p_path.size()<node_q_path.size())path_len=node_p_path.size();elsepath_len=node_q_path.size();TreeNode *result=0;for(int i=0;i<path_len;++i){if(node_p_path[i]==node_q_path[i])//相同節(jié)點(diǎn) result=node_p_path[i];//相同則更新,直到距離根節(jié)點(diǎn)最遠(yuǎn) }return result; }void pre_order(TreeNode * node,TreeNode *search,vector<TreeNode*> &path, vector<TreeNode*> &result,//返回路徑結(jié)果 int &finish){if(!node||finish==1)//為空或者找到則返回 return;path.push_back(node);//保存節(jié)點(diǎn)數(shù)值if(node==search){finish=1;result=path;//找到則節(jié)點(diǎn)路徑保存 } pre_order(node->left,search,path,result,finish);pre_order(node->right,search,path,result,finish);path.pop_back();//結(jié)束遍歷node時(shí),node節(jié)點(diǎn)彈出path棧 }};int main(){//暴力構(gòu)造樹TreeNode a(3);TreeNode b(5);TreeNode c(1);TreeNode d(6);TreeNode e(2);TreeNode f(0);TreeNode g(8);TreeNode h(7);TreeNode i(4);a.left=&b;//連接節(jié)點(diǎn)a.right=&c;c.left=&f;c.right=&g;b.left=&d;b.right=&e;e.left=&h;e.right=&i;solution solve;TreeNode *result=solve.lowestCommonAncestor(&a,&d,&i) ;cout<<"the Lowest Common Ancestor: "<<result->val<<endl; result=solve.lowestCommonAncestor(&a,&h,&i) ;cout<<"the Lowest Common Ancestor: "<<result->val<<endl; result=solve.lowestCommonAncestor(&a,&d,&g) ;cout<<"the Lowest Common Ancestor: "<<result->val<<endl; return 0;}

測試結(jié)果


題目來源:力扣(LeetCode)
鏈接:請點(diǎn)擊https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree

總結(jié)

以上是生活随笔為你收集整理的Leetcode236 最近公共祖先-二叉树两次遍历的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。