"Hello World!" in C

Sort by

recency

|

638 Discussions

|

  • + 0 comments

    int main() {

    char s[100];
    scanf("%[^\n]%*c", &s);
    
    printf("Hello, World! \n");
    printf(s);    
    return 0;
    

    }

  • + 0 comments
    //Boundary Traversal of a Binary Tree
    #include <iostream>
    #include <vector>
    using namespace std;
    
    struct Node {
        int val;
        Node *left, *right;
        Node(int x) : val(x), left(nullptr), right(nullptr) {}
    };
    
    void leftBoundary(Node* node, vector<int>& res) {
        if (!node || (!node->left && !node->right)) return;
        res.push_back(node->val);
        if (node->left) leftBoundary(node->left, res);
        else leftBoundary(node->right, res);
    }
    
    void leaves(Node* node, vector<int>& res) {
        if (!node) return;
        if (!node->left && !node->right) res.push_back(node->val);
        leaves(node->left, res);
        leaves(node->right, res);
    }
    
    void rightBoundary(Node* node, vector<int>& res) {
        if (!node || (!node->left && !node->right)) return;
        if (node->right) rightBoundary(node->right, res);
        else rightBoundary(node->left, res);
        res.push_back(node->val);
    }
    
    vector<int> boundaryTraversal(Node* root) {
        vector<int> res;
        if (!root) return res;
        res.push_back(root->val);
        leftBoundary(root->left, res);
        leaves(root->left, res);
        leaves(root->right, res);
        rightBoundary(root->right, res);
        return res;
    }
    
    int main() {
        Node* root = new Node(1);
        root->left = new Node(2);
        root->right = new Node(3);
        root->left->left = new Node(4);
        root->left->right = new Node(5);
        root->right->left = new Node(6);
        root->right->right = new Node(7);
        vector<int> res = boundaryTraversal(root);
        for (int x : res) cout << x << " ";
        return 0;
    }
    
  • + 0 comments
    //Maximum Level Sum in a Binary Tree
    #include <iostream>
    #include <queue>
    using namespace std;
    
    struct Node {
        int val;
        Node *left, *right;
        Node(int x) : val(x), left(nullptr), right(nullptr) {}
    };
    
    int maxLevelSum(Node* root) {
        if (!root) return 0;
        queue<Node*> q;
        q.push(root);
        int maxSum = root->val;
        while (!q.empty()) {
            int levelSum = 0, size = q.size();
            for (int i = 0; i < size; i++) {
                Node* node = q.front();
                q.pop();
                levelSum += node->val;
                if (node->left) q.push(node->left);
                if (node->right) q.push(node->right);
            }
            maxSum = max(maxSum, levelSum);
        }
        return maxSum;
    }
    
    int main() {
        Node* root = new Node(1);
        root->left = new Node(2);
        root->right = new Node(3);
        root->left->left = new Node(4);
        root->left->right = new Node(5);
        root->right->right = new Node(8);
        cout << maxLevelSum(root);
        return 0;
    }
    
  • + 0 comments
    #include <iostream>
    #include <vector>
    #include <queue>
    using namespace std;
    
    vector<vector<int>> tree;
    vector<int> dist;
    
    void bfs(int start, int &farthest) {
        queue<int> q;
        q.push(start);
        dist[start] = 0;
        while (!q.empty()) {
            int node = q.front();
            q.pop();
            for (int neighbor : tree[node]) {
                if (dist[neighbor] == -1) {
                    dist[neighbor] = dist[node] + 1;
                    q.push(neighbor);
                    if (dist[neighbor] > dist[farthest]) farthest = neighbor;
                }
            }
        }
    }
    
    int longestPath(int n) {
        dist.assign(n + 1, -1);
        int farthest = 1;
        bfs(1, farthest);
        dist.assign(n + 1, -1);
        bfs(farthest, farthest);
        return dist[farthest];
    }
    
    int main() {
        int n, u, v;
        cin >> n;
        tree.assign(n + 1, vector<int>());
        for (int i = 0; i < n - 1; i++) {
            cin >> u >> v;
            tree[u].push_back(v);
            tree[v].push_back(u);
        }
        cout << longestPath(n);
        return 0;
    }
    
  • + 0 comments

    include

    include

    include

    include

    int main() {

    char s[100];
    printf("Hello, World!\n");
    fgets(s,sizeof(s),stdin); 
    printf(s) ;
     return 0;
    

    }