Sort by

recency

|

716 Discussions

|

  • + 0 comments

    include

    include

    include

    include

    include

    using namespace std; vector leftNode, rightNode; int swapLevel;

    void traverse(int node=1){ if (node == -1) return; traverse(leftNode[node]); cout << node << " "; traverse(rightNode[node]); if (node == 1) cout << endl; }

    void swap(int level=1, int node=1) { if (node == -1) return; if (level % swapLevel == 0) { int tmp = leftNode[node]; leftNode[node] = rightNode[node]; rightNode[node] = tmp; } swap(level+1, leftNode[node]); swap(level+1, rightNode[node]); }

    int main() { int count;
    cin>>count; leftNode.push_back(0); rightNode.push_back(0); while(count--){ int L, R; cin>>L>>R; leftNode.push_back(L); rightNode.push_back(R); } cin>>count; while(count--){ cin >> swapLevel; swap(); traverse(); } return 0; }

  • + 0 comments

    SOLUTION

    include

    include

    struct node { int id; int depth; struct node *left, *right; };

    void inorder(struct node* tree) { if(tree == NULL) return;

    inorder(tree->left);
    printf("%d ",tree->id);
    inorder((tree->right));
    

    }

    int main(void) { int no_of_nodes, i = 0; int l,r, max_depth,k; struct node* temp = NULL; scanf("%d",&no_of_nodes); struct node* tree = (struct node *) calloc(no_of_nodes , sizeof(struct node)); tree[0].depth = 1; while(i < no_of_nodes ) { tree[i].id = i+1; scanf("%d %d",&l,&r); if(l == -1) tree[i].left = NULL; else { tree[i].left = &tree[l-1]; tree[i].left->depth = tree[i].depth + 1; max_depth = tree[i].left->depth; }

         if(r ==  -1)
            tree[i].right = NULL;
        else
             {
                 tree[i].right = &tree[r-1];
                 tree[i].right->depth = tree[i].depth + 1;
                 max_depth = tree[i].right->depth+2;
             }
    
    i++;
    }
    
    scanf("%d", &i);
    while(i--)
    {
        scanf("%d",&l);
        r = l;
        while(l <= max_depth)
        {
            for(k = 0;k < no_of_nodes; ++k)
            {
                if(tree[k].depth == l)
                {
                    temp = tree[k].left;
                    tree[k].left = tree[k].right;
                    tree[k].right = temp;
                }
            }
            l = l + r;
        }
        inorder(tree);
        printf("\n");
    }
    
    
    
    return 0;
    

    }

  • + 0 comments

    SOLUTION

    include

    include

    struct node { int id; int depth; struct node *left, *right; };

    void inorder(struct node* tree) { if(tree == NULL) return;

    inorder(tree->left);
    printf("%d ",tree->id);
    inorder((tree->right));
    

    }

    int main(void) { int no_of_nodes, i = 0; int l,r, max_depth,k; struct node* temp = NULL; scanf("%d",&no_of_nodes); struct node* tree = (struct node *) calloc(no_of_nodes , sizeof(struct node)); tree[0].depth = 1; while(i < no_of_nodes ) { tree[i].id = i+1; scanf("%d %d",&l,&r); if(l == -1) tree[i].left = NULL; else { tree[i].left = &tree[l-1]; tree[i].left->depth = tree[i].depth + 1; max_depth = tree[i].left->depth; }

         if(r ==  -1)
            tree[i].right = NULL;
        else
             {
                 tree[i].right = &tree[r-1];
                 tree[i].right->depth = tree[i].depth + 1;
                 max_depth = tree[i].right->depth+2;
             }
    
    i++;
    }
    
    scanf("%d", &i);
    while(i--)
    {
        scanf("%d",&l);
        r = l;
        while(l <= max_depth)
        {
            for(k = 0;k < no_of_nodes; ++k)
            {
                if(tree[k].depth == l)
                {
                    temp = tree[k].left;
                    tree[k].left = tree[k].right;
                    tree[k].right = temp;
                }
            }
            l = l + r;
        }
        inorder(tree);
        printf("\n");
    }
    
    
    
    return 0;
    

    }

  • + 0 comments

    Python 3 Version, it's mostly the same as g503420777's code but it's derived from trying to analyze as normal node traversal

    sys.setrecursionlimit(2000)
    def swapNodes(indexes, queries):
        # Write your code here
        result = []
        for k in queries:
            res = []
            swap(indexes, 1, 1, k, res)
            #inOrder(indexes, 1, res)
            result.append(res)
        return result
    
    def swap(nodes, depth, idx, k, res):
        if idx == -1:
            return
        if depth % k == 0:
            nodes[idx - 1][0], nodes[idx - 1][1] = nodes[idx - 1][1], nodes[idx - 1][0]
        swap(nodes, depth + 1, nodes[idx - 1][0], k, res)
        res.append(idx)
        swap(nodes, depth + 1, nodes[idx - 1][1], k, res)
            
    def inOrder(nodes, idx, result):
        left = nodes[idx - 1][0]
        right = nodes[idx - 1][1]
        if left != -1:
            inOrder(nodes, left, result)
        result.append(idx)
        if right != -1:
            inOrder(nodes, right, result)
    
  • + 0 comments

    Here's my solution in C#, which passed all 11 test cases. For each node, you need to keep track of its index, children, and depth, the last one being important for the swap operation since you need to swap every node whose depth is a multiple of the number specified by each query.

    Since the swaps affect nodes individually, you can perform the in-order traversal and the child swaps using the same recursion function.

    public class Node {
            public int root;
            public int left;
            public int right;
            public int depth;
            
            public Node(int n, int a, int b) {
                root = n;
                left = a;
                right = b;
                depth = -1;
            }
        }
    
        public static List<List<int>> swapNodes(List<List<int>> indexes, List<int> queries)
        {
            List<List<int>> swappedTree = new List<List<int>>();
            List<Node> nodeList = new List<Node>();
            
            for (int i = 0; i < indexes.Count; i++) {
                List<int> index = indexes[i];
                int a = index[0]-1;
                int b = index[1]-1;
                //Console.WriteLine(a + ", " + b);
                nodeList.Add(new Node(i, a, b));
            }
            
            for (int i = 0; i < nodeList.Count; i++) {
                Node currNode = nodeList[i];
                int currentdepth = nodeList[i].depth > 0 ? nodeList[i].depth : 1;
                int a = currNode.left;
                int b = currNode.right;
                if (nodeList[i].depth < 0) nodeList[i].depth = currentdepth;
                if (a > 0) nodeList[a].depth = currentdepth+1;
                if (b > 0) nodeList[b].depth = currentdepth+1;
            }
            
            while (queries.Count > 0) {
                int query = queries[0];
                List<int> inOrderTraversalList = inOrderTraversal(nodeList, 0, query);
                swappedTree.Add(inOrderTraversalList);
                queries.RemoveAt(0);
            }
            
            return swappedTree;
        }
        
        public static List<int> inOrderTraversal(List<Node> nodeList, int nodeIndex, int query) {
            List<int> inOrderTraversalList = new List<int>();
            //Console.WriteLine(nodeIndex);
            if (nodeList[nodeIndex].depth % query == 0) {
                int a = nodeList[nodeIndex].left;
                nodeList[nodeIndex].left = nodeList[nodeIndex].right;
                nodeList[nodeIndex].right = a;
            }
            
            if (nodeList[nodeIndex].left > 0) inOrderTraversalList.AddRange(inOrderTraversal(nodeList, nodeList[nodeIndex].left, query));
            inOrderTraversalList.Add(nodeIndex+1);
            if (nodeList[nodeIndex].right > 0) inOrderTraversalList.AddRange(inOrderTraversal(nodeList, nodeList[nodeIndex].right, query));
            
            return inOrderTraversalList;
        }