二叉树祖先节点_二叉树的祖先

二叉树祖先节点

Problem statement:

问题陈述:

Given a Binary Tree and a target key, write a function that prints all the ancestors of the key in the given binary tree.

给定二叉树和目标键,编写一个函数,以打印给定二叉树中键的所有祖先

Example:

例:

Let's the tree be like following:

让树如下所示:

Ancestors in Binary Tree
    Let for node value 12:
Ancestors are:
7, 5, 8
While for node value 7:
Ancestors are:
5, 8

Solution

What is Ancestors?

什么是祖先?

For any node n,
Its ancestors are the nodes which are on the path between roots to node n

对于任何节点n
它的祖先是位于根到节点n之间的路径上的节点

Thus for the above examples,

因此,对于以上示例,

Example 1:

范例1:

    Node is 12 //represented by value
Root to the node path is
8->5->7->12
Thus the ancestors are 7, 5, 8

Example 2:

范例2:

    Node is 7 //represented by value
Root to the node path is
8->5->7
Thus the ancestors are 5, 8

Algorithm:

算法:

FUNCTION printAncestors(Node *root, int target)
IF(!root)
return false;
IF( (root->left && root->left->data==target) ||
(root->right && root->right->data==target ) || 
printAncestors(root->left,target)|| 
printAncestors(root->right,target)){
Print root->data;
return true;
END IF
return false;
END FUNCTION

That simply means we are doing kind of DFS

这仅表示我们正在执行某种DFS

For a currentnode to be ancestor of the target node the conditions are:

为了使currentnode成为目标节点的祖先,条件是:

1.  If the target node is its child node (either left child or right child)
//condition
IF((root->left && root->left->data==target) ||
(root->right && root->right->data==target ))
2.  If any of the two subtree of the current node contain ancestor of the target 
node then the current node is also an ancestor. 
//condition
IF(printAncestors(root->left, target)|| 
printAncestors(root->right, target))

Example with explanation:

带有说明的示例:

For target node 7:
Root 8 is ancestor on condition: its left subtree contains ancestor 5
5 is ancestor since target node is its right child
Thus ancestors are:
5, 8 //in order

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
//tree node
class Node{ 
public:
int data;
Node *left;
Node *right;
};
bool printAncestors(Node *root, int target)
{
if(!root)
return false;
if(	(root->left && root->left->data==target) ||
(root->right && root->right->data==target ) || 
printAncestors(root->left,target)|| 
printAncestors(root->right,target)){
cout<<root->data<<" ";
return true;
}
return false;
}
//creating new nodes
Node* newnode(int data){  
Node* node = (Node*)malloc(sizeof(Node)); 
node->data = data; 
node->left = NULL; 
node->right = NULL; 
return(node); 
} 
int main() { 
//**same tree is builted as shown in example**
cout<<"tree in the example is build here"<<endl;
//building the tree like as in the example
Node *root=newnode(8); 
root->left= newnode(5); 
root->right= newnode(4); 
root->right->right=newnode(11);
root->right->right->left=newnode(3);
root->left->left=newnode(9); 
root->left->right=newnode(7);
root->left->right->left=newnode(1);
root->left->right->right=newnode(12);
root->left->right->right->left=newnode(2);
int s;
cout<<"enter input value to find ancestors......"<<endl;
cin>>s;
printAncestors(root,s);
return 0; 
}

Output

输出量

tree in the example is build here
enter input value to find ancestors......
7
5 8

翻译自: https://www.includehelp.com/icp/ancestors-in-binary-tree.aspx

二叉树祖先节点

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543859.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

CALayer精讲

CALayer精讲 CALayer包含在QuartzCore框架中&#xff0c;这是一个跨平台的框架&#xff0c;既可以用在iOS中又可以用在Mac OS X中。后面要学Core Animation就应该先学好Layer&#xff08;层&#xff09;。 我们看一下UIView与Layer之间的关系图&#xff08;图片来源于网络&…

rofl用什么播放_ROFL的完整形式是什么?

rofl用什么播放ROFL&#xff1a;笑在地板上滚动 (ROFL: Rolling On Floor Laughing) ROFL is an abbreviation of Rolling on Floor Laughing. ROFL is a very trendy internet slang between youngsters and used in text messaging, instant messaging, chatting, and social…

gif 格式 完整 检查_GIF的完整格式是什么?

gif 格式 完整 检查GIF&#xff1a;图形交换格式 (GIF: Graphics Interchange Format) GIF is an abbreviation of Graphics Interchange Format. It is extensively used for animations and still images on the World Wide Web. The image is set out is bitmap image and i…

Java基础_05

2019独角兽企业重金招聘Python工程师标准>>> 1&#xff1a;boolean运算符号 || 与 | && 与 &的区别。 Equals与innstanceof 1&#xff1a;java中的方法。方法的定义&#xff0c;参数、返回值、调用方式。 2&#xff1a;方法调用与参数传递、Static方…

Android Studio 之下载安装

2019独角兽企业重金招聘Python工程师标准>>> 目录[-] 背景Android Studio VS Eclipse下载创建HelloWorld项目背景 相信大家对Android Studio已经不陌生了&#xff0c;Android Studio是Google于2013 I/O大会针对Android开发推出的新的开发工具&#xff0c;目前很多开…

模拟UIWebView

2019独角兽企业重金招聘Python工程师标准>>> // // ViewController.m // 模拟UIWebView // // Created by dc0061 on 15/12/10. // Copyright © 2015年 dc0061. All rights reserved. //#import "ViewController.h"interface ViewController ()&…

4g 中bis代表什么_BIS的完整形式是什么?

4g 中bis代表什么BIS&#xff1a;印度标准局 (BIS: Bureau of Indian Standards) BIS is an abbreviation of the Bureau of Indian Standards. It is the National Standard Body of India which is operating in the groundwork and execution of the standards, certificati…

Feature selection

原文:http://scikit-learn.org/stable/modules/feature_selection.html The classes in the sklearn.feature_selection module can be used for feature selection/dimensionality reduction on sample sets, either to improve estimators’ accuracy scores or to boost the…

ronald aai_AAI的完整形式是什么?

ronald aaiAAI&#xff1a;印度机场管理局 (AAI: Airport Authority of India) AAI is an abbreviation of the Airport Authority of India. It operates under the Ministry of Civil Aviation. It is in charge of creating, crafting, maintaining and enhancing the civil…

使用Eclipse-Maven-git做Java开发(13)--导入git仓库的代码到eclipse

2019独角兽企业重金招聘Python工程师标准>>> 前面讲到了怎么使用osc的git服务进行代码托管。至此&#xff0c;我们已经可以使用git进行文件的版本管理了&#xff0c;甚至可以进行不需要IDE的编程了&#xff0c;但是我们绝大多数时候还是需要IDE的&#xff0c;接下来…

python 三维图直方图_Python | 阶梯直方图

python 三维图直方图A histogram is a graphical technique or a type of data representation using bars of different heights such that each bar groups numbers into ranges (bins or buckets). Taller the bar higher the data falls in that bin. A Histogram is one o…

ExtJS4.2学习(21)动态菜单与表格数据展示操作总结篇2

运行效果&#xff1a; 此文介绍了根据操作左侧菜单在右面板展示相应内容。 一、主页 先看一下跳转主页的方式&#xff1a;由在webapp根目录下的index.jsp跳转至demo的index.jsp 下面是demo的index.jsp的代码 <% page language"java" contentType"text/html; …

jQuery之call()方法的使用

最近在做项目时候&#xff0c;写了几行关于DOM操作的代码&#xff0c;在方法中使用了this&#xff0c;在后期重构的时候&#xff0c;想将这段分离出来做成一个方法。 最开始想的很简单&#xff0c;就直接分离出来使用方法名称调用即可。 但是实际操作的时候没有效果&#xff0c…

github的使用

GitHub操作总结 : 总结看不明白就看下面的详细讲解. GitHub操作流程 : 第一次提交 : 方案一 : 本地创建项目根目录, 然后与远程GitHub关联, 之后的操作一样; -- 初始化git仓库 :git init ; -- 提交改变到缓存 :git commit -m description ; -- 本地git仓库关联GitHub仓库 : g…

sql更改完整模式报错_SQL的完整形式是什么?

sql更改完整模式报错SQL&#xff1a;结构化查询语言 (SQL: Structured Query Language) SQL is an abbreviation of Structured Query Language. It is a programming language developed and designed for handling structured data in Relational Database Management System…

基于微服务架构,改造企业核心系统之实践

2019独角兽企业重金招聘Python工程师标准>>> 1. 背景与挑战 随着公司国际化战略的推行以及本土业务的高速发展&#xff0c;后台支撑系统已经不堪重负。在吞吐量、稳定性以及可扩展性上都无法满足日益增长的业务需求。对于每10万元额度的合同&#xff0c;从销售团队…

bkg bnc_BNC的完整形式是什么?

bkg bncBNC&#xff1a;刺刀Neill–Concelman (BNC: Bayonet Neill–Concelman) BNC is an abbreviation of "Bayonet Neill–Concelman". BNC是“刺刀Neill–Concelman”的缩写 。 It is also known as "British Naval Connector" or "Bayonet Nut …

使用visio 提示此UML形状所在的绘图页不是UML模型图的一部分 请问这个问题怎么解决?...

解决方法新建->选择软件与数据库模板->选择UML模型图->注意&#xff1a;如果不选择UML模型图的话&#xff0c;可能会出现无法编辑形状文本&#xff0c;提示“此UML形状所在的绘图页不是UML模型图的一部分&#xff0c;该形状设计用于利用UML模型图模板创建的绘图”关注…

tgc 什么意思 tgt_TGT的完整形式是什么?

tgc 什么意思 tgtTGT&#xff1a;训练有素的研究生老师 (TGT: Trained Graduate Teacher) TGT is an abbreviation of Trained Graduate Teacher. It is a title, not a teaching program that is given to a graduate person who has done completion of training in teaching…

svn的使用(Mac)

2019独角兽企业重金招聘Python工程师标准>>> 从服务器下载代码 在终端中输入svn checkout svn://localhost/mycode --username用户名 --password密码 /Users/apple/Documents/code指令意思&#xff1a;将服务器中mycode仓库的内容下载到/Users/apple/Documents/myCo…