牛客 在二叉树中找到两个节点的最近公共祖先

题目链接:https://www.nowcoder.com/practice/c75deef6d4bf40249c785f240dad4247?tpId=101&tqId=33244&tPage=1&rp=1&ru=/ta/programmer-code-interview-guide&qru=/ta/programmer-code-interview-guide/question-ranking

题目大意

  略。

分析

  直接把两个节点拔到同一高度后一起往上取父节点直到两个节点相等即可。

代码如下

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3  
  4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  5 #define Rep(i,n) for (int i = 0; i < (int)(n); ++i)
  6 #define For(i,s,t) for (int i = (int)(s); i <= (int)(t); ++i)
  7 #define rFor(i,t,s) for (int i = (int)(t); i >= (int)(s); --i)
  8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 12  
 13 #define pr(x) cout << #x << " = " << x << "  "
 14 #define prln(x) cout << #x << " = " << x << endl
 15  
 16 #define LOWBIT(x) ((x)&(-x))
 17  
 18 #define ALL(x) x.begin(),x.end()
 19 #define INS(x) inserter(x,x.begin())
 20 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
 21 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c 
 22 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
 23 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
 24  
 25 #define ms0(a) memset(a,0,sizeof(a))
 26 #define msI(a) memset(a,0x3f,sizeof(a))
 27 #define msM(a) memset(a,-1,sizeof(a))
 28 
 29 #define MP make_pair
 30 #define PB push_back
 31 #define ft first
 32 #define sd second
 33  
 34 template<typename T1, typename T2>
 35 istream &operator>>(istream &in, pair<T1, T2> &p) {
 36     in >> p.first >> p.second;
 37     return in;
 38 }
 39  
 40 template<typename T>
 41 istream &operator>>(istream &in, vector<T> &v) {
 42     for (auto &x: v)
 43         in >> x;
 44     return in;
 45 }
 46 
 47 template<typename T>
 48 ostream &operator<<(ostream &out, vector<T> &v) {
 49     Rep(i, v.size()) out << v[i] << " 
"[i == v.size() - 1];
 50     return out;
 51 }
 52  
 53 template<typename T1, typename T2>
 54 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 55     out << "[" << p.first << ", " << p.second << "]" << "
";
 56     return out;
 57 }
 58 
 59 inline int gc(){
 60     static const int BUF = 1e7;
 61     static char buf[BUF], *bg = buf + BUF, *ed = bg;
 62     
 63     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
 64     return *bg++;
 65 } 
 66 
 67 inline int ri(){
 68     int x = 0, f = 1, c = gc();
 69     for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
 70     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
 71     return x*f;
 72 }
 73 
 74 template<class T>
 75 inline string toString(T x) {
 76     ostringstream sout;
 77     sout << x;
 78     return sout.str();
 79 }
 80 
 81 inline int toInt(string s) {
 82     int v;
 83     istringstream sin(s);
 84     sin >> v;
 85     return v;
 86 }
 87 
 88 //min <= aim <= max
 89 template<typename T>
 90 inline bool BETWEEN(const T aim, const T min, const T max) {
 91     return min <= aim && aim <= max;
 92 }
 93  
 94 typedef long long LL;
 95 typedef unsigned long long uLL;
 96 typedef vector< int > VI;
 97 typedef vector< bool > VB;
 98 typedef vector< char > VC;
 99 typedef vector< double > VD;
100 typedef vector< string > VS;
101 typedef vector< LL > VL;
102 typedef vector< VI > VVI;
103 typedef vector< VB > VVB;
104 typedef vector< VS > VVS;
105 typedef vector< VL > VVL;
106 typedef vector< VVI > VVVI;
107 typedef vector< VVL > VVVL;
108 typedef pair< int, int > PII;
109 typedef pair< LL, LL > PLL;
110 typedef pair< int, string > PIS;
111 typedef pair< string, int > PSI;
112 typedef pair< string, string > PSS;
113 typedef pair< double, double > PDD;
114 typedef vector< PII > VPII;
115 typedef vector< PLL > VPLL;
116 typedef vector< VPII > VVPII;
117 typedef vector< VPLL > VVPLL;
118 typedef vector< VS > VVS;
119 typedef map< int, int > MII;
120 typedef unordered_map< int, int > uMII;
121 typedef map< LL, LL > MLL;
122 typedef map< string, int > MSI;
123 typedef map< int, string > MIS;
124 typedef set< int > SI;
125 typedef stack< int > SKI;
126 typedef deque< int > DQI;
127 typedef queue< int > QI;
128 typedef priority_queue< int > PQIMax;
129 typedef priority_queue< int, VI, greater< int > > PQIMin;
130 const double EPS = 1e-8;
131 const LL inf = 0x7fffffff;
132 const LL infLL = 0x7fffffffffffffffLL;
133 const LL mod = 1e9 + 7;
134 const int maxN = 5e5 + 7;
135 const LL ONE = 1;
136 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
137 const LL oddBits = 0x5555555555555555;
138 
139 struct TreeNode {
140     int lch = 0, rch = 0, val = 0, fa = 0, level = 0;
141 };
142 
143 int N, root, ans, o1, o2;
144 TreeNode tree[maxN];
145 
146 int main(){
147     //freopen("MyOutput.txt","w",stdout);
148     //freopen("input.txt","r",stdin);
149     //INIT();
150     scanf("%d%d", &N, &root);
151     tree[root].level = 1;
152     Rep(i, N) {
153         int fa, lch, rch;
154         scanf("%d%d%d", &fa, &lch, &rch);
155         
156         tree[fa].lch = lch;
157         tree[fa].rch = rch;
158         
159         tree[lch].level = tree[rch].level = tree[fa].level + 1;
160         tree[lch].fa = fa;
161         tree[rch].fa = fa;
162     }
163     scanf("%d%d", &o1, &o2);
164     
165     if(tree[o1].level > tree[o2].level) swap(o1, o2);
166     while(tree[o1].level < tree[o2].level) o2 = tree[o2].fa;
167     while(o1 != o2) {
168         o1 = tree[o1].fa;
169         o2 = tree[o2].fa;
170     }
171     printf("%d
", o1);
172     return 0;
173 }
View Code
原文地址:https://www.cnblogs.com/zaq19970105/p/11363593.html