打印二叉树中的所有完整节点

给定一个二叉树,print所有节点都将是完整节点。 完整节点 是同时具有左、右子节点的非空节点。 例如:

null
Input :    10          /           8    2        /    /       3   5 7Output : 10 8Input :   1         /         2   3           /           4   6     Output : 1 3

这是一个简单的问题。我们做任何一种转换( 订单、预订单、订单、, 级别顺序遍历),并将具有模式左和右子节点的节点保持为非空。

C++

// A C++ program to find the all full nodes in
// a given binary tree
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *left, *right;
};
Node *newNode( int data)
{
Node *temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// Traverses given tree in Inorder fashion and
// prints all nodes that have both children as
// non-empty.
void findFullNode(Node *root)
{
if (root != NULL)
{
findFullNode(root->left);
if (root->left != NULL && root->right != NULL)
cout << root->data << " " ;
findFullNode(root->right);
}
}
// Driver program to test above function
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->right->left = newNode(5);
root->right->right = newNode(6);
root->right->left->right = newNode(7);
root->right->right->right = newNode(8);
root->right->left->right->left = newNode(9);
findFullNode(root);
return 0;
}


JAVA

// Java program to find the all full nodes in
// a given binary tree
public class FullNodes {
// Traverses given tree in Inorder fashion and
// prints all nodes that have both children as
// non-empty.
public static void findFullNode(Node root)
{
if (root != null )
{
findFullNode(root.left);
if (root.left != null && root.right != null )
System.out.print(root.data+ " " );
findFullNode(root.right);
}
}
public static void main(String args[]) {
Node root = new Node( 1 );
root.left = new Node( 2 );
root.right = new Node( 3 );
root.left.left = new Node( 4 );
root.right.left = new Node( 5 );
root.right.right = new Node( 6 );
root.right.left.right = new Node( 7 );
root.right.right.right = new Node( 8 );
root.right.left.right.left = new Node( 9 );
findFullNode(root);
}
}
/* A binary tree node */
class Node
{
int data;
Node left, right;
Node( int data)
{
left=right= null ;
this .data=data;
}
};
//This code is contributed by Gaurav Tiwari


Python3

# Python3 program to find the all
# full nodes in a given binary tree
# Binary Tree Node
""" utility that allocates a newNode
with the given key """
class newNode:
# Construct to create a newNode
def __init__( self , key):
self .data = key
self .left = None
self .right = None
# Traverses given tree in Inorder
# fashion and prints all nodes that
# have both children as non-empty.
def findFullNode(root) :
if (root ! = None ) :
findFullNode(root.left)
if (root.left ! = None and
root.right ! = None ) :
print (root.data, end = " " )
findFullNode(root.right)
# Driver Code
if __name__ = = '__main__' :
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.left = newNode( 4 )
root.right.left = newNode( 5 )
root.right.right = newNode( 6 )
root.right.left.right = newNode( 7 )
root.right.right.right = newNode( 8 )
root.right.left.right.left = newNode( 9 )
findFullNode(root)
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)


C#

// C# program to find the all full nodes in
// a given binary tree
using System;
public class FullNodes
{
// Traverses given tree in Inorder fashion and
// prints all nodes that have both children as
// non-empty.
static void findFullNode(Node root)
{
if (root != null )
{
findFullNode(root.left);
if (root.left != null && root.right != null )
Console.Write(root.data + " " );
findFullNode(root.right);
}
}
public static void Main(String []args)
{
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.left = new Node(5);
root.right.right = new Node(6);
root.right.left.right = new Node(7);
root.right.right.right = new Node(8);
root.right.left.right.left = new Node(9);
findFullNode(root);
}
}
/* A binary tree node */
class Node
{
public int data;
public Node left, right;
public Node( int data)
{
left = right = null ;
this .data = data;
}
};
// This code is contributed by 29AjayKumar


Javascript

<script>
// JavaScript program to find the all full nodes in
// a given binary tree
/* A binary tree node */
class Node
{
constructor(data)
{
this .left= this .right= null ;
this .data=data;
}
}
// Traverses given tree in Inorder fashion and
// prints all nodes that have both children as
// non-empty.
function findFullNode(root)
{
if (root != null )
{
findFullNode(root.left);
if (root.left != null && root.right != null )
document.write(root.data+ " " );
findFullNode(root.right);
}
}
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.left = new Node(5);
root.right.right = new Node(6);
root.right.left.right = new Node(7);
root.right.right.right = new Node(8);
root.right.left.right.left = new Node(9);
findFullNode(root);
// This code is contributed by rag2127
</script>


输出:

1 3

时间复杂性: O(n)

空间复杂性: O(n)对于倾斜树情况下的递归堆栈空间

本文由 拉凯什·库马尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享