二叉树祖先节点_二叉树的祖先
二叉樹祖先節(jié)點
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.
給定二叉樹和目標鍵,編寫一個函數(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, 8Solution
解
What is Ancestors?
什么是祖先?
For any node n,
Its ancestors are the nodes which are on the path between roots to node n
對于任何節(jié)點n ,
它的祖先是位于根到節(jié)點n之間的路徑上的節(jié)點
Thus for the above examples,
因此,對于以上示例,
Example 1:
范例1:
Node is 12 //represented by valueRoot to the node path is8->5->7->12Thus the ancestors are 7, 5, 8Example 2:
范例2:
Node is 7 //represented by valueRoot to the node path is8->5->7Thus the ancestors are 5, 8Algorithm:
算法:
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 FUNCTIONThat simply means we are doing kind of DFS
這僅表示我們正在執(zhí)行某種DFS
For a currentnode to be ancestor of the target node the conditions are:
為了使currentnode成為目標節(jié)點的祖先,條件是:
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 ++實現(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é)點
總結(jié)
以上是生活随笔為你收集整理的二叉树祖先节点_二叉树的祖先的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ibm mq的交互命令模式_IBM的完整
- 下一篇: oracle 静态监听 端口,侦听动态注