Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print the nodes at the odd levels of a binary tree. #594

Open
rathoresrikant opened this issue Oct 27, 2018 · 5 comments
Open

Print the nodes at the odd levels of a binary tree. #594

rathoresrikant opened this issue Oct 27, 2018 · 5 comments

Comments

@rathoresrikant
Copy link
Owner

Details:
Programming language : Any
Directory : Data Structures/Trees

@Rajat-dey
Copy link
Contributor

Working on it.

@Rajat-dey
Copy link
Contributor

The is resolved and the code is pulled under a binary tree in Data Structure

@ruanchaves
Copy link
Contributor

Python #608

@vivekarya99
Copy link

using namespace std;

struct Node {
int data;
Node* left, *right;
};

void printOddNodes(Node *root, bool isOdd = true)
{
// If empty tree
if (root == NULL)
return;

// If current node is of odd level 
if (isOdd) 
   cout << root->data << " " ; 

// Recur for children with isOdd 
// switched. 
printOddNodes(root->left, !isOdd); 
printOddNodes(root->right, !isOdd); 

}

// Utility method to create a node
struct Node* newNode(int data)
{
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}

// Driver code
int main()
{
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printOddNodes(root);

return 0; 

}

@yokaiemporer
Copy link
Contributor

#901

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants