Question

The Problem Complete the printFromButtom function that accepts a BST TreeNode and returns the nodes value from left to right, level by level from leaf to root. This function will return vector<vector int which similar to a 2-D array. Function std: reverse (myvector.begin myVector en might be helpful. Definition for a binary tree node: struct TreeNode int val TreeNode *left; TreeNode right Examples Example 1 Sample Binary Search Tree: 9 20 15 7 Result: [15,7], 19,20] [3]

Coding Language: C++

Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {}

Definition for a binary tree node:

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
};

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
struct TreeNode
{
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
};
using namespace std;
vector< vector<int> > printFromBottom(TreeNode* root)
{
    vector< vector<int> > ans;
    queue <TreeNode *> Q;
    Q.push(NULL);
    Q.push(root);
vector<int> temp;
    while (Q.empty() == false)
    {
        root = Q.front();
        Q.pop();
        if(root==NULL){
         std::reverse(temp.begin(), temp.end());
            ans.push_back(temp);
            if(Q.empty() == true)
                break;
            Q.push(NULL);
            temp.clear();
      
        }else{
          temp.push_back(root->val);
        if (root->right)
            Q.push(root->right);

        if (root->left)
            Q.push(root->left);
        }
      
    }

    std::reverse(ans.begin(), ans.end());
        for(int i=0;i<ans.size();++i){
            cout << endl;
            for(int j=0;j<ans[i].size();++j)
                cout << ans[i][j] << " ";
        }
      
    return ans;
}
TreeNode* newNode(int data)
{
    TreeNode* temp = new TreeNode;
    temp->val = data;
    temp->left = NULL;
    temp->right = NULL;

    return (temp);
}

/* Driver program to test above functions*/
int main()
{
    struct TreeNode *root = newNode(3);
    root->left        = newNode(9);
    root->right       = newNode(20);
    root->right->left = newNode(15);
    root->right->right = newNode(7);

    cout << "Print from Bottom \n";
    printFromBottom(root);

    return 0;
}



==================================================================================
See output

日ロ+ 《 * New Project-201 70310 BAコroot Compile ! | Execute i l > Share Code main.cppx struct TreeNode *root -newNode(3); root-

Thanks, let me know if there is any concern.

Add a comment
Know the answer?
Add Answer to:
Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PROMPT: Consider a binary tree (not necessarily a binary search tree) with node structure. QUESTION: Prove that findMax works by mathematical induction. struct Node int val; struct Node * left; struc...

    PROMPT: Consider a binary tree (not necessarily a binary search tree) with node structure. QUESTION: Prove that findMax works by mathematical induction. struct Node int val; struct Node * left; struct Node* right; The following function findMax returns the largest value 'val in the tree; and returns -1 if the tree is empty. You may assume that all the values 'val' in the tree are nonnegative. struct Node * findMax(struct Node root) if (rootNULL) return -1; maxval = root->val; maxval...

  • By definition, the height of a node in a binary tree is the number of edges...

    By definition, the height of a node in a binary tree is the number of edges along the longest path from the node to any leaf. Assume the following node structure struct TreeNode int data; node Type right; // points to right child node Type "Left; // points to left child ) Write a recursive function that takes a pointer to a node in a binary tree and returns its height. Note: the height of a leaf node is 0...

  • By definition, the height of a node in a binary tree is the number of edges...

    By definition, the height of a node in a binary tree is the number of edges along the longest path from the node to any leaf. Assume the following node structure struct TreeNode! int data; node Type right; // points to right child node Type "left; // points to left child }; Write a recursive function that takes a pointer to a node in a binary tree and returns its height. Note: the height of a leaf node is o...

  • /* * struct for a single node in a binary tree. data contains the int *...

    /* * struct for a single node in a binary tree. data contains the int * stored in this node. left and right contain pointers to the left and * right subtrees respectively. * * All of the ints stored in the left subtree is smaller than data. * All of the ints stored in the right subtree is larger than data. */ struct node { int data; struct node *left; struct node *right; }; typedef struct node node; Write...

  • Write a function int levelSearch(Node* root, int key) that takes as input the root node of...

    Write a function int levelSearch(Node* root, int key) that takes as input the root node of a Binary Search tree and a key. The function searches the key in the BST and returns the level of the found node. If the key is not found in the tree, return -1. The level starts at 0 for the root node and increases from top to bottom. We have defined the following node C++ Node class for you: class Node { public:         ...

  • Given the declaration: struct TreeNode {     int data;     TreeNode...

    Given the declaration: struct TreeNode {     int data;     TreeNode* left;     TreeNode* right; }; Write a function to test and see if a given complete binary tree is a (max) heap: bool isAHeap(TreeNode *root) On this, and on any subsequent questions where you are asked to give code, please use the Formatted paragraph style rather than Normal.

  • Given the declaration: struct TreeNode { int data; TreeNode* left; TreeNode* right; };

    Given the declaration:struct TreeNode {    int data;    TreeNode* left;    TreeNode* right;};Write a function to test a tree and return if every node has a 0 or 1 non-null children, i.e., no node has 2 non-null children.bool isDegenerate(TreeNode *root)On this, and on any subsequent questions where you are asked to give code, please use the Formatted paragraph style rather than Normal.

  • The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode...

    The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode root) { } public static void main(String[] args) { TreeNode a = new TreeNode(1); TreeNode b = new TreeNode(2); TreeNode c = new TreeNode(3); a.left = b; a.right = c; System.out.println(isValidBST(a)); TreeNode d = new TreeNode(2); TreeNode e = new TreeNode(1); TreeNode f = new TreeNode(3); d.left = e; d.right = f; System.out.println(isValidBST(d)); } } TreeNode.java class TreeNode { int val; TreeNode left; TreeNode...

  • Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing...

    Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing left, right, and parent pointers, in addition to holding an Data struct value Tree struct containing a pointer to the root of the tree A function declaration for a function that allocates a tree, and initializes the root to NULL o o o A...

  • Write a recursive function (C++) that searches a binary tree that is NOT ordered like a...

    Write a recursive function (C++) that searches a binary tree that is NOT ordered like a BST. In other words, there is no ordering relationship among the parent, child or sibling node values. Use the following prototype and data structure: struct node { node *left; node *right; int val; }; // First parameter: pointer to a node // Second parameter: the value to find bool searchValue(node *, int); The function searchValue() should return true if the integer value in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT