We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<string>#include<cstring>#include<iostream>#include<iomanip>#include<vector>#include<algorithm>#include<sstream>#include<map>#include<set>#include<cmath>#include<fstream>usingnamespacestd;usingll=longlong;constintMOD=1e9+7;/*I remember that night when you and I held each other all night*///BST//search,insert,delete: O(logn) average//worst:O(n)structNode{intval;Node*left,*right;Node(intx){val=x;left=right=NULL;}};Node*insert(Node*root,intx){if(root==NULL)returnnewNode(x);if(x<root->val)root->left=insert(root->left,x);elseroot->right=insert(root->right,x);returnroot;}voidinorder(Node*root){if(root==NULL)return;inorder(root->left);cout<<root->val<<' ';inorder(root->right);}intmain(){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);Node*root=NULL;intn;cin>>n;for(inti=0;i<n;i++){intx;cin>>x;root=insert(root,x);}inorder(root);}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tree: Inorder Traversal
You are viewing a single comment's thread. Return to all comments →