日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/11 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二叉树祖先节点_二叉树的祖先 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

二叉樹祖先節(jié)點(diǎn)

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.

給定二叉樹和目標(biāo)鍵,編寫一個(gè)函數(shù),以打印給定二叉樹中鍵的所有祖先 。

Example:

例:

Let's the tree be like following:

讓樹如下所示:

Let for node value 12:Ancestors are:7, 5, 8While 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

對(duì)于任何節(jié)點(diǎn)n ,
它的祖先是位于根到節(jié)點(diǎn)n之間的路徑上的節(jié)點(diǎn)

Thus for the above examples,

因此,對(duì)于以上示例,

Example 1:

范例1:

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

Example 2:

范例2:

Node is 7 //represented by valueRoot to the node path is8->5->7Thus 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 IFreturn false; END FUNCTION

That simply means we are doing kind of DFS

這僅表示我們正在執(zhí)行某種DFS

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

為了使currentnode成為目標(biāo)節(jié)點(diǎn)的祖先,條件是:

1. If the target node is its child node (either left child or right child)//conditionIF((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. //conditionIF(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 .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

C++ Implementation:

C ++實(shí)現(xiàn):

#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 exampleNode *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

二叉樹祖先節(jié)點(diǎn)

總結(jié)

以上是生活随笔為你收集整理的二叉树祖先节点_二叉树的祖先的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。