zoukankan      html  css  js  c++  java
  • 面试题07 二叉树两节点的最低公共祖先 [树]

    #include <iostream>  // 前提 :二叉树不具有父节点  如果具有父节点则是 T型链表问题 -> 先走几步的问题。 
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <vector>
    #include <stack>
    #include <deque>
    #include <queue>
    #include <bitset>
    #include <list>
    #include <map>
    #include <set>
    #include <iterator>
    #include <algorithm>
    #include <functional>
    #include <utility>
    #include <sstream>
    #include <climits>
    #include <cassert>
    #define BUG puts("here!!!");
    
    
    using namespace std;
    struct Node {
    	int value;
    	Node *lchild, *rchild;
    };
    list<Node*> L1;
    list<Node*> L2;
    bool getNodePath(Node* pr, Node* tar, list<Node*>& L) {
    	if(pr == NULL) return false;
    	if(pr == tar) return true;
    	L.push_back(pr);
    	bool flag = false;
    	if(pr->lchild != NULL) flag = getNodePath(pr->lchild, tar, L);
    	if(!flag && pr->rchild) flag = getNodePath(pr->rchild, tar, L);
    	if(!flag) L.pop_back();
    	return flag;
    }
    Node* getCom(const list<Node*>& L1, const list<Node*>& L2) {
    	list<Node*>::const_iterator it1 = L1.begin();
    	list<Node*>::const_iterator it2 = L2.begin();
    	Node* pLast = NULL;
    	while(it1 != L1.end() && it2 != L2.end()) {
    		if(*it1 != *it2) break;
    		pLast = *it1;
    		++it1;
    		++it2;
    	}
    	return pLast;
    }
    int main() {
    	return 0;
    }
  • 相关阅读:
    任务墙(6月3日)
    燃尽图(6月3日)
    6.1-6.2小结
    5月28日任务进展
    个人感悟
    代码评审
    如何用ZBrush确定头部五官的位置
    ZBrush中的纹理-水手该怎样进行绘制
    怎样对ZBrush中的材料进行渲染和着色
    快速熟悉Zbrush中的四种裁切笔刷
  • 原文地址:https://www.cnblogs.com/robbychan/p/3787169.html
Copyright © 2011-2022 走看看