二叉树最近公共父节点
2017-05-24 13:27
246 查看
#include <bits/stdc++.h> using namespace std; struct TreeNode { int val; TreeNode*left; TreeNode*right; TreeNode(int x): val(x),left(NULL),right(NULL) {} }; int k; int x,y; void create(TreeNode*root,int n) { if(n==1)return; root->left=new TreeNode(root->val-pow(2,n-2)); root->right=new TreeNode(root->val+pow(2,n-2)); create(root->left,n-1); create(root->right,n-1); } //针对二叉排序树 TreeNode*findTreeNode(TreeNode*root,int n) { if(root->val==n)return root; else if(root->val<n)return findTreeNode(root->right,n); else findTreeNode(root->left,n); } //方法一:针对所有二叉树结构,复杂度较高 TreeNode*findCommonParent(TreeNode*root,TreeNode*p,TreeNode*q) { if(root==NULL||p==root||q==root)return root; TreeNode*leftNode=findCommonParent(root->left,p,q); TreeNode*rightNode=findCommonParent(root->right,p,q); if(leftNode&&rightNode)return root; else if(leftNode)return leftNode; else return rightNode; } //方法二:针对二叉树平排序结构,复杂度较低 TreeNode*findCommonParent1(TreeNode*root,TreeNode*p,TreeNode*q) { int min,max; if(p->val<q->val)min=p->val,max=q->val; else min=q->val,max=p->val; while(root) { if(root->val>=min&&root->val<=max)return root; else if(root->val<min&&root->val<max)root=root->right; else root=root->left; } return NULL; } //获得根节点到某一节点路径 bool getNodePath(TreeNode*root,TreeNode*p,list<TreeNode*>&l) { if(root==NULL||p==NULL)return false; l.push_back(root); if(root==p)return true; bool found=false; if(root->left)found=getNodePath(root->left,p,l); if(!found&&root->right)found=getNodePath(root->right,p,l); if(!found)l.pop_back(); return found; } //方法三:针对所有二叉树结构,复杂度较低 TreeNode*findCommonParent2(TreeNode*root,TreeNode*p,TreeNode*q) { list<TreeNode*>l1,l2; getNodePath(root,p,l1); getNodePath(root,q,l2); reverse(l1.begin(),l1.end()); reverse(l2.begin(),l2.end()); list<TreeNode*>::iterator it1=l1.begin(),it2=l2.begin(); if(l1.size()>l2.size()) { for(int i=0; i<l1.size()-l2.size(); i++) it1++; } else { for(int i=0; i<l2.size()-l1.size(); i++) it2++; } for(; it1!=l1.end()&&it2!=l2.end(); it1++,it2++) { if(*it1==*it2) { return *it1; break; } } return NULL; } //前序遍历 void print(TreeNode*root) { if(root==NULL)return; cout<<root->val<<endl; print(root->left); print(root-&g 4000 t;right); } int main() { while(cin>>k>>x>>y) { TreeNode*root=new TreeNode(pow(2,k-1)); create(root,k); TreeNode*p=findTreeNode(root,x); TreeNode*q=findTreeNode(root,y); TreeNode*commonParent=findCommonParent(root,p,q); cout<<"方法一:"<<commonParent->val<<endl; TreeNode*commonParent1=findCommonParent1(root,p,q); cout<<"方法二:"<<commonParent1->val<<endl; TreeNode*commonParent2=findCommonParent2(root,p,q); cout<<"方法三:"<<commonParent2->val<<endl; } return 0; }
相关文章推荐
- 二叉树中两个节点的最近公共父节点
- 二叉树最近公共父节点
- 二叉树最近公共父节点
- 【二叉树】求二叉树中两个节点的最近公共父节点
- 求二叉树中两个节点的最近公共父节点
- 二叉树中两个节点的最近公共父节点
- 二叉树中两个节点的最近公共父节点 非二叉排序树
- 二叉树最近公共父节点
- 二叉树中两个节点的最近公共父节点
- 二叉树中两个节点的最近公共父节点
- 二叉树最近公共父节点
- 二叉树最近公共父节点
- 二叉树中两个节点的最近公共父节点 使用二叉排序树
- 算法题16 二叉树最近公共父节点
- 【二叉树12】求一颗二叉树中两个节点的最近公共父节点
- 二叉树中两个节点的最近公共父节点
- [转]程序员面试100题之十六:二叉树中两个节点的最近公共父节点
- 笔试算法题(24):找出出现次数超过一半的元素 & 二叉树最近公共父节点
- 二叉树(12)----查找两个节点最低祖先节点(或最近公共父节点等),递归和非递归
- 二叉树中两个节点的最近公共父节点