瀏覽代碼

June 1st problem done

master
Lachlan Jacob 5 年之前
父節點
當前提交
84886a1d4b
共有 3 個文件被更改,包括 71 次插入0 次删除
  1. 41
    0
      problems/226/main.c
  2. 25
    0
      problems/226/problem.txt
  3. 5
    0
      problems/226/run.sh

+ 41
- 0
problems/226/main.c 查看文件

@@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>

// Definition for a binary tree node.
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};

void printTree(struct TreeNode* tree) {
if (tree != NULL) {
printf(" %d ", tree->val);
printTree(tree->left);
printTree(tree->right);
}
}


struct TreeNode* invertTree(struct TreeNode* root){
if (root != NULL) {
struct TreeNode* temp = root->right;
root->right = root->left;
root->left = temp;
invertTree(root->left);
invertTree(root->right);
}
return root;
}

int main() {
struct TreeNode l = { 2, NULL, NULL };
struct TreeNode r = { 3, NULL, NULL };
struct TreeNode tree = { 1, &l, &r};
struct TreeNode* inverted = invertTree(&tree);
printf("Expected: 1 3 2 \n");
printf("Got:");
printTree(inverted);
printf("\n");
return 0;
}

+ 25
- 0
problems/226/problem.txt 查看文件

@@ -0,0 +1,25 @@
Invert a binary tree.

Example:

Input:

4
/ \
2 7
/ \ / \
1 3 6 9

Output:

4
/ \
7 2
/ \ / \
9 6 3 1

Trivia:
This problem was inspired by this original tweet by Max Howell:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.


+ 5
- 0
problems/226/run.sh 查看文件

@@ -0,0 +1,5 @@
#!/bin/bash

gcc -o main main.c
./main
rm main

Loading…
取消
儲存