Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Boundary Traversal of binary tree

#include <bits/stdc++.h>

using namespace std;

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

bool isLeaf(node * root) {
  return !root -> left && !root -> right;
}

void addLeftBoundary(node * root, vector < int > & res) {
  node * cur = root -> left;
  while (cur) {
    if (!isLeaf(cur)) res.push_back(cur -> data);
    if (cur -> left) cur = cur -> left;
    else cur = cur -> right;
  }
}
void addRightBoundary(node * root, vector < int > & res) {
  node * cur = root -> right;
  vector < int > tmp;
  while (cur) {
    if (!isLeaf(cur)) tmp.push_back(cur -> data);
    if (cur -> right) cur = cur -> right;
    else cur = cur -> left;
  }
  for (int i = tmp.size() - 1; i >= 0; --i) {
    res.push_back(tmp[i]);
  }
}

void addLeaves(node * root, vector < int > & res) {
  if (isLeaf(root)) {
    res.push_back(root -> data);
    return;
  }
  if (root -> left) addLeaves(root -> left, res);
  if (root -> right) addLeaves(root -> right, res);
}

vector < int > printBoundary(node * root) {
  vector < int > res;
  if (!root) return res;

  if (!isLeaf(root)) res.push_back(root -> data);

  addLeftBoundary(root, res);

  // add leaf nodes
  addLeaves(root, res);

  addRightBoundary(root, res);
  return res;
}

struct node * newNode(int data) {
  struct node * node = (struct node * ) malloc(sizeof(struct node));
  node -> data = data;
  node -> left = NULL;
  node -> right = NULL;

  return (node);
}

int main() {

  struct node * root = newNode(1);
  root -> left = newNode(2);
  root -> left -> left = newNode(3);
  root -> left -> left -> right = newNode(4);
  root -> left -> left -> right -> left = newNode(5);
  root -> left -> left -> right -> right = newNode(6);
  root -> right = newNode(7);
  root -> right -> right = newNode(8);
  root -> right -> right -> left = newNode(9);
  root -> right -> right -> left -> left = newNode(10);
  root -> right -> right -> left -> right = newNode(11);

  vector < int > boundaryTraversal;
  boundaryTraversal = printBoundary(root);

  cout << "The Boundary Traversal is : ";
  for (int i = 0; i < boundaryTraversal.size(); i++) {
    cout << boundaryTraversal[i] << " ";
  }
  return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: convert c++ program to c online 
Cpp :: bitmap rotate 90 deg 
Cpp :: android call custom managed method from native code 
Cpp :: online c++ graphics compiler 
Cpp :: cast c++ 
Cpp :: c++ string not printing 
Cpp :: is vowel c++/c 
Cpp :: std::string(size_t , char ) constructor: 
Cpp :: how to check code execution time in visual studio c++ 
Cpp :: delete node in a linked list leetcode 
Cpp :: c++ login 
Cpp :: c++ struktura kolejki 
Cpp :: max stack 
Cpp :: iterator c++ 
Cpp :: cpp map contains 
Cpp :: palindrome string 
Cpp :: how to change the type of something in c++ 
Cpp :: aliasing c++ 
Cpp :: c++ error 0xC0000005 
Cpp :: how to shorten code using using c++ in class with typename 
C :: swapping of two numbers in c without temporary variable 
C :: check dns server in linux 
C :: random number in c 
C :: Reduce fractions in C 
C :: addition of two matrix in c 
C :: write a program in c to check whether the number is armstrong or not 
C :: servo motor arduino 
C :: stdio 
C :: what is system function in c 
C :: accessing elements of 1d array using pointers 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =