当前位置: 首页>>代码示例>>C++>>正文


C++ Solution::LCA方法代码示例

本文整理汇总了C++中Solution::LCA方法的典型用法代码示例。如果您正苦于以下问题:C++ Solution::LCA方法的具体用法?C++ Solution::LCA怎么用?C++ Solution::LCA使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Solution的用法示例。


在下文中一共展示了Solution::LCA方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main(){
	Solution c;
	TreeNode *root = new TreeNode(3);
	root->left = new TreeNode(5);
	root->right = new TreeNode(1);
	root->left->left = new TreeNode(6);
	root->left->right = new TreeNode(2);
	root->right->left = new TreeNode(0);
	root->right->right = new TreeNode(8);
	root->left->right->left = new TreeNode(7);
	root->left->right->right = new TreeNode(4);
	TreeNode *p = root->left, *q = root->left->right->right;
	cout << p->val << " " << q->val << endl;
	TreeNode *s = c.LCA(root,p,q);//c.lowestCommonAncestor(root, p, q);
	cout << s->val;
	return 0;
}
开发者ID:Qzhenlong,项目名称:LeetCode,代码行数:17,代码来源:LowestCommonAncestorofaBinaryTree.cpp

示例2: main

int main(int argc, char** argv){
  Solution S;
  
  int* arr = (int *)malloc(sizeof(int)* (argc -1));
  for(int i = 1; i < argc; i ++){
     arr[i-1] = atoi(argv[i]);
  }
  
  //int arr [] = { 12 ,123, 12312, 42112, 13421, 1234,312, 314};
  //int n = sizeof(arr)/sizeof(int);
 
  TreeNode * root = S.sortedArrToBST(arr, argc -1);
  S.print(root);

  cout<<"isBST? "<<S.isBST(root)<<endl;
  //cout<<"Maximum number of the BST: "<<S.maxNum(root)<<endl;

  cout<<"Get the maximum number for the left subtree of the root: "<<S.getMax(root->left)<<endl;

  cout<<"Get the maximum number of the BST: "<<S.getMax(root)<<endl;

  cout<<"Verify the BST: "<<S.verify(root)<<endl;
  
  

  cout<<"The second maximum number of the BST: "<<S.getSecondMax(root)<<endl;

  cout<<"Print type in two nums in the BST: "<<endl;
  
  int n1, n2;
  cin>>n1>>n2;
  
  TreeNode * ans = S.LCA(root, n1, n2);
  cout<<"You are typing in : "<<n1<<" "<<n2<<endl;
  if(ans == NULL)
    cout<<"No lowest common ancestor for those two nodes !"<<endl;
  else
    cout<<"Lowest common ancestor for those two nodes is: "<<ans->val<<endl;
  
  cout<<"Printing the maximum num for each path from root to leaf: "<<endl;
  S.printMaximumOnPath(root, INT_MIN);
  free(arr);
  return 0;

}
开发者ID:jinyyy666,项目名称:internview-prep,代码行数:45,代码来源:convertArr2BST.cpp


注:本文中的Solution::LCA方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。