diff options
Diffstat (limited to 'top-interview-questions/medium/trees_and_graphs')
4 files changed, 176 insertions, 0 deletions
diff --git a/top-interview-questions/medium/trees_and_graphs/01_binary_tree_inorder_traversal.cc b/top-interview-questions/medium/trees_and_graphs/01_binary_tree_inorder_traversal.cc new file mode 100644 index 0000000..6a2762b --- /dev/null +++ b/top-interview-questions/medium/trees_and_graphs/01_binary_tree_inorder_traversal.cc | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | /** | ||
| 2 | * Definition for a binary tree node. | ||
| 3 | * struct TreeNode { | ||
| 4 | * int val; | ||
| 5 | * TreeNode *left; | ||
| 6 | * TreeNode *right; | ||
| 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
| 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
| 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
| 10 | * }; | ||
| 11 | */ | ||
| 12 | class Solution { | ||
| 13 | public: | ||
| 14 | void inorder(TreeNode* node, vector<int>& vals) { | ||
| 15 | if (node == nullptr) return; | ||
| 16 | |||
| 17 | inorder(node->left, vals); | ||
| 18 | vals.push_back(node->val); | ||
| 19 | inorder(node->right, vals); | ||
| 20 | } | ||
| 21 | |||
| 22 | vector<int> inorderTraversal(TreeNode* root) { | ||
| 23 | vector<int> vals; | ||
| 24 | inorder(root, vals); | ||
| 25 | return vals; | ||
| 26 | } | ||
| 27 | }; | ||
diff --git a/top-interview-questions/medium/trees_and_graphs/02_binary_tree_zigzag_level_order_traversal.cc b/top-interview-questions/medium/trees_and_graphs/02_binary_tree_zigzag_level_order_traversal.cc new file mode 100644 index 0000000..2abb093 --- /dev/null +++ b/top-interview-questions/medium/trees_and_graphs/02_binary_tree_zigzag_level_order_traversal.cc | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | /** | ||
| 2 | * Definition for a binary tree node. | ||
| 3 | * struct TreeNode { | ||
| 4 | * int val; | ||
| 5 | * TreeNode *left; | ||
| 6 | * TreeNode *right; | ||
| 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
| 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
| 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
| 10 | * }; | ||
| 11 | */ | ||
| 12 | class Solution { | ||
| 13 | public: | ||
| 14 | void push(vector<const TreeNode*>& level, vector<int>& values, const TreeNode* node) { | ||
| 15 | level.push_back(node); | ||
| 16 | values.push_back(node->val); | ||
| 17 | } | ||
| 18 | |||
| 19 | void zigZag(bool leftRight, const vector<const TreeNode*>& level, vector<vector<int>>& values) { | ||
| 20 | vector<const TreeNode*> next_level; | ||
| 21 | vector<int> next_values; | ||
| 22 | |||
| 23 | if (leftRight) { | ||
| 24 | for (int i = level.size()-1; i >= 0; --i) { | ||
| 25 | const TreeNode* node = level[i]; | ||
| 26 | if (node->left) push(next_level, next_values, node->left); | ||
| 27 | if (node->right) push(next_level, next_values, node->right); | ||
| 28 | } | ||
| 29 | } | ||
| 30 | else { | ||
| 31 | for (int i = level.size()-1; i >= 0; --i) { | ||
| 32 | const TreeNode* node = level[i]; | ||
| 33 | if (node->right) push(next_level, next_values, node->right); | ||
| 34 | if (node->left) push(next_level, next_values, node->left); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | if (!next_level.empty()) { | ||
| 39 | values.push_back(next_values); | ||
| 40 | zigZag(!leftRight, next_level, values); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | vector<vector<int>> zigzagLevelOrder(TreeNode* root) { | ||
| 45 | if (root == nullptr) return {}; | ||
| 46 | |||
| 47 | vector<const TreeNode*> level = {root}; | ||
| 48 | vector<vector<int>> values = {{root->val}}; | ||
| 49 | |||
| 50 | zigZag(false, level, values); | ||
| 51 | return values; | ||
| 52 | } | ||
| 53 | }; | ||
diff --git a/top-interview-questions/medium/trees_and_graphs/03_construct_binary_tree_from_preorder_and_inorder_traversal.cc b/top-interview-questions/medium/trees_and_graphs/03_construct_binary_tree_from_preorder_and_inorder_traversal.cc new file mode 100644 index 0000000..95e3655 --- /dev/null +++ b/top-interview-questions/medium/trees_and_graphs/03_construct_binary_tree_from_preorder_and_inorder_traversal.cc | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | /** | ||
| 2 | * Definition for a binary tree node. | ||
| 3 | * struct TreeNode { | ||
| 4 | * int val; | ||
| 5 | * TreeNode *left; | ||
| 6 | * TreeNode *right; | ||
| 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
| 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
| 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
| 10 | * }; | ||
| 11 | */ | ||
| 12 | class Solution { | ||
| 13 | public: | ||
| 14 | TreeNode* build(const vector<int>& preorder, const vector<int>& inorder, | ||
| 15 | int& preIdx, int leftIdx, int rightIdx) { | ||
| 16 | // Base case. | ||
| 17 | if (leftIdx > rightIdx) { | ||
| 18 | return nullptr; | ||
| 19 | } | ||
| 20 | |||
| 21 | // Root value. | ||
| 22 | const int root = preorder[preIdx++]; | ||
| 23 | |||
| 24 | // Base case. | ||
| 25 | if (leftIdx == rightIdx) { | ||
| 26 | return new TreeNode{root, nullptr, nullptr}; | ||
| 27 | } | ||
| 28 | // Recursive case. | ||
| 29 | else { | ||
| 30 | // Find the root in the inorder array. | ||
| 31 | int inorderRootIdx = -1; | ||
| 32 | for (int i = 0; i < inorder.size(); ++i) { | ||
| 33 | if (inorder[i] == root) { | ||
| 34 | inorderRootIdx = i; | ||
| 35 | break; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | // Build recursively. | ||
| 39 | TreeNode* left = build(preorder, inorder, preIdx, leftIdx, inorderRootIdx-1); | ||
| 40 | TreeNode* right = build(preorder, inorder, preIdx, inorderRootIdx+1, rightIdx); | ||
| 41 | return new TreeNode{root, left, right}; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { | ||
| 46 | int preIdx = 0; | ||
| 47 | return build(preorder, inorder, preIdx, 0, preorder.size()-1); | ||
| 48 | } | ||
| 49 | }; | ||
diff --git a/top-interview-questions/medium/trees_and_graphs/04_populating_next_right_pointers_in_each_node.cc b/top-interview-questions/medium/trees_and_graphs/04_populating_next_right_pointers_in_each_node.cc new file mode 100644 index 0000000..17a5874 --- /dev/null +++ b/top-interview-questions/medium/trees_and_graphs/04_populating_next_right_pointers_in_each_node.cc | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | /* | ||
| 2 | // Definition for a Node. | ||
| 3 | class Node { | ||
| 4 | public: | ||
| 5 | int val; | ||
| 6 | Node* left; | ||
| 7 | Node* right; | ||
| 8 | Node* next; | ||
| 9 | |||
| 10 | Node() : val(0), left(NULL), right(NULL), next(NULL) {} | ||
| 11 | |||
| 12 | Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {} | ||
| 13 | |||
| 14 | Node(int _val, Node* _left, Node* _right, Node* _next) | ||
| 15 | : val(_val), left(_left), right(_right), next(_next) {} | ||
| 16 | }; | ||
| 17 | */ | ||
| 18 | |||
| 19 | class Solution { | ||
| 20 | public: | ||
| 21 | // The obvious solution is to do a BFS and connect the nodes in each level. | ||
| 22 | // However, the problem asks us to use "constant space" (depth of tree). | ||
| 23 | |||
| 24 | void connectLeftRight(Node* leftTree, Node* rightTree) { | ||
| 25 | while (leftTree && rightTree) { | ||
| 26 | leftTree->next = rightTree; | ||
| 27 | |||
| 28 | leftTree = leftTree->right; | ||
| 29 | rightTree = rightTree->left; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | Node* connect(Node* root) { | ||
| 34 | if (root == nullptr) { | ||
| 35 | return root; | ||
| 36 | } | ||
| 37 | |||
| 38 | connect(root->left); | ||
| 39 | connect(root->right); | ||
| 40 | |||
| 41 | if (root->left) { // Non-leaf node. | ||
| 42 | connectLeftRight(root->left, root->right); | ||
| 43 | } | ||
| 44 | |||
| 45 | return root; | ||
| 46 | } | ||
| 47 | }; | ||
