原文:
给定一个二叉树和一个节点,打印给定节点的所有表兄弟。请注意,不应打印兄弟姐妹。 例:
input : root of below tree
1
/ \
2 3
/ \ / \
4 5 6 7
and pointer to a node say 5.
output : 6, 7
首先使用这里讨论的方法找到给定节点的级别。找到级别后,我们可以使用这里打印给定级别的所有节点。唯一要照顾的是,弟妹不要印。为了处理这个问题,我们将打印功能更改为首先检查同级节点,只有当它不是同级节点时才打印节点。 下面是上面想法的实现。
c
// c program to print cousins of a node
#include
using namespace std;
// a binary tree node
struct node
{
int data;
node *left, *right;
};
// a utility function to create a new
// binary tree node
node *newnode(int item)
{
node *temp = new node;
temp->data = item;
temp->left = temp->right = null;
return temp;
}
/* it returns level of the node if it is
present in tree, otherwise returns 0.*/
int getlevel(node *root, node *node, int level)
{
// base cases
if (root == null)
return 0;
if (root == node)
return level;
// if node is present in left subtree
int downlevel = getlevel(root->left,
node, level 1);
if (downlevel != 0)
return downlevel;
// if node is not present in left subtree
return getlevel(root->right, node, level 1);
}
/* print nodes at a given level such that
sibling of node is not printed if it exists */
void printgivenlevel(node* root, node *node, int level)
{
// base cases
if (root == null || level < 2)
return;
// if current node is parent of a node
// with given level
if (level == 2)
{
if (root->left == node || root->right == node)
return;
if (root->left)
cout << root->left->data << " ";
if (root->right)
cout << root->right->data;
}
// recur for left and right subtrees
else if (level > 2)
{
printgivenlevel(root->left, node, level - 1);
printgivenlevel(root->right, node, level - 1);
}
}
// this function prints cousins of a given node
void printcousins(node *root, node *node)
{
// get level of given node
int level = getlevel(root, node, 1);
// print nodes of given level.
printgivenlevel(root, node, level);
}
// driver code
int main()
{
node *root = newnode(1);
root->left = newnode(2);
root->right = newnode(3);
root->left->left = newnode(4);
root->left->right = newnode(5);
root->left->right->right = newnode(15);
root->right->left = newnode(6);
root->right->right = newnode(7);
root->right->left->right = newnode(8);
printcousins(root, root->left->right);
return 0;
}
// this code is contributed
// by akanksha rai
c
// c program to print cousins of a node
#include
#include
// a binary tree node
struct node
{
int data;
node *left, *right;
};
// a utility function to create a new binary
// tree node
node *newnode(int item)
{
node *temp = new node;
temp->data = item;
temp->left = temp->right = null;
return temp;
}
/* it returns level of the node if it is present
in tree, otherwise returns 0.*/
int getlevel(node *root, node *node, int level)
{
// base cases
if (root == null)
return 0;
if (root == node)
return level;
// if node is present in left subtree
int downlevel = getlevel(root->left, node, level 1);
if (downlevel != 0)
return downlevel;
// if node is not present in left subtree
return getlevel(root->right, node, level 1);
}
/* print nodes at a given level such that sibling of
node is not printed if it exists */
void printgivenlevel(node* root, node *node, int level)
{
// base cases
if (root == null || level < 2)
return;
// if current node is parent of a node with
// given level
if (level == 2)
{
if (root->left == node || root->right == node)
return;
if (root->left)
printf("%d ", root->left->data);
if (root->right)
printf("%d ", root->right->data);
}
// recur for left and right subtrees
else if (level > 2)
{
printgivenlevel(root->left, node, level-1);
printgivenlevel(root->right, node, level-1);
}
}
// this function prints cousins of a given node
void printcousins(node *root, node *node)
{
// get level of given node
int level = getlevel(root, node, 1);
// print nodes of given level.
printgivenlevel(root, node, level);
}
// driver program to test above functions
int main()
{
node *root = newnode(1);
root->left = newnode(2);
root->right = newnode(3);
root->left->left = newnode(4);
root->left->right = newnode(5);
root->left->right->right = newnode(15);
root->right->left = newnode(6);
root->right->right = newnode(7);
root->right->left->right = newnode(8);
printcousins(root, root->left->right);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to print cousins of a node
class gfg {
// a binary tree node
static class node
{
int data;
node left, right;
}
// a utility function to create a new binary
// tree node
static node newnode(int item)
{
node temp = new node();
temp.data = item;
temp.left = null;
temp.right = null;
return temp;
}
/* it returns level of the node if it is present
in tree, otherwise returns 0.*/
static int getlevel(node root, node node, int level)
{
// base cases
if (root == null)
return 0;
if (root == node)
return level;
// if node is present in left subtree
int downlevel = getlevel(root.left, node, level 1);
if (downlevel != 0)
return downlevel;
// if node is not present in left subtree
return getlevel(root.right, node, level 1);
}
/* print nodes at a given level such that sibling of
node is not printed if it exists */
static void printgivenlevel(node root, node node, int level)
{
// base cases
if (root == null || level < 2)
return;
// if current node is parent of a node with
// given level
if (level == 2)
{
if (root.left == node || root.right == node)
return;
if (root.left != null)
system.out.print(root.left.data " ");
if (root.right != null)
system.out.print(root.right.data " ");
}
// recur for left and right subtrees
else if (level > 2)
{
printgivenlevel(root.left, node, level-1);
printgivenlevel(root.right, node, level-1);
}
}
// this function prints cousins of a given node
static void printcousins(node root, node node)
{
// get level of given node
int level = getlevel(root, node, 1);
// print nodes of given level.
printgivenlevel(root, node, level);
}
// driver program to test above functions
public static void main(string[] args)
{
node root = newnode(1);
root.left = newnode(2);
root.right = newnode(3);
root.left.left = newnode(4);
root.left.right = newnode(5);
root.left.right.right = newnode(15);
root.right.left = newnode(6);
root.right.right = newnode(7);
root.right.left.right = newnode(8);
printcousins(root, root.left.right);
}
}
python 3
# python3 program to print cousins of a node
# a utility function to create a new
# binary tree node
class newnode:
def __init__(self, item):
self.data = item
self.left = self.right = none
# it returns level of the node if it is
# present in tree, otherwise returns 0.
def getlevel(root, node, level):
# base cases
if (root == none):
return 0
if (root == node):
return level
# if node is present in left subtree
downlevel = getlevel(root.left, node,
level 1)
if (downlevel != 0):
return downlevel
# if node is not present in left subtree
return getlevel(root.right, node, level 1)
# prnodes at a given level such that
# sibling of node is not printed if
# it exists
def printgivenlevel(root, node, level):
# base cases
if (root == none or level < 2):
return
# if current node is parent of a
# node with given level
if (level == 2):
if (root.left == node or
root.right == node):
return
if (root.left):
print(root.left.data, end = " ")
if (root.right):
print(root.right.data, end = " ")
# recur for left and right subtrees
elif (level > 2):
printgivenlevel(root.left, node, level - 1)
printgivenlevel(root.right, node, level - 1)
# this function prints cousins of a given node
def printcousins(root, node):
# get level of given node
level = getlevel(root, node, 1)
# prnodes of given level.
printgivenlevel(root, node, level)
# driver code
if __name__ == '__main__':
root = newnode(1)
root.left = newnode(2)
root.right = newnode(3)
root.left.left = newnode(4)
root.left.right = newnode(5)
root.left.right.right = newnode(15)
root.right.left = newnode(6)
root.right.right = newnode(7)
root.right.left.right = newnode(8)
printcousins(root, root.left.right)
# this code is contributed by pranchalk
c
// c# program to print cousins of a node
using system;
public class gfg
{
// a binary tree node
class node
{
public int data;
public node left, right;
}
// a utility function to create
// a new binary tree node
static node newnode(int item)
{
node temp = new node();
temp.data = item;
temp.left = null;
temp.right = null;
return temp;
}
/* it returns level of the node
if it is present in tree,
otherwise returns 0.*/
static int getlevel(node root,
node node, int level)
{
// base cases
if (root == null)
return 0;
if (root == node)
return level;
// if node is present in left subtree
int downlevel = getlevel(root.left, node, level 1);
if (downlevel != 0)
return downlevel;
// if node is not present in left subtree
return getlevel(root.right, node, level 1);
}
/* print nodes at a given level
such that sibling of node is
not printed if it exists */
static void printgivenlevel(node root,
node node, int level)
{
// base cases
if (root == null || level < 2)
return;
// if current node is parent of a node with
// given level
if (level == 2)
{
if (root.left == node || root.right == node)
return;
if (root.left != null)
console.write(root.left.data " ");
if (root.right != null)
console.write(root.right.data " ");
}
// recur for left and right subtrees
else if (level > 2)
{
printgivenlevel(root.left, node, level - 1);
printgivenlevel(root.right, node, level - 1);
}
}
// this function prints cousins of a given node
static void printcousins(node root, node node)
{
// get level of given node
int level = getlevel(root, node, 1);
// print nodes of given level.
printgivenlevel(root, node, level);
}
// driver code
public static void main(string[] args)
{
node root = newnode(1);
root.left = newnode(2);
root.right = newnode(3);
root.left.left = newnode(4);
root.left.right = newnode(5);
root.left.right.right = newnode(15);
root.right.left = newnode(6);
root.right.right = newnode(7);
root.right.left.right = newnode(8);
printcousins(root, root.left.right);
}
}
// this code is contributed rajput-ji
java 描述语言
输出:
6 7
时间复杂度: o(n) 我们能用单次遍历解决这个问题吗?请参考以下文章 本文由 shivam gupta 供稿。如果你喜欢极客博客并想投稿,你也可以写一篇文章并把你的文章邮寄到 review-team@geeksforgeeks.org。看到你的文章出现在极客博客pg电子试玩链接主页上,帮助其他极客。 发现有不正确的地方请写评论,或者想分享更多以上讨论话题的信息
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处