mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 09:54:07 +00:00
day 7.1: 94-binary-tree-inorder-traversal
This commit is contained in:
parent
81bd6f46c3
commit
32366c1e21
11
94-binary-tree-inorder-traversal/driver.cpp
Normal file
11
94-binary-tree-inorder-traversal/driver.cpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
TreeNode n3 = TreeNode(3);
|
||||||
|
TreeNode n2 = TreeNode(2, &n3, nullptr);
|
||||||
|
TreeNode n1 = TreeNode(1, nullptr, &n2);
|
||||||
|
Solution soln;
|
||||||
|
cout << "Inorder traversal correct? " << soln.test(&n1, vector<int>{1, 3, 2}) << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
19
94-binary-tree-inorder-traversal/soln.cpp
Normal file
19
94-binary-tree-inorder-traversal/soln.cpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
vector<int> Solution::inorderTraversal(TreeNode *root)
|
||||||
|
{
|
||||||
|
vector<int> tmp;
|
||||||
|
if (root != nullptr)
|
||||||
|
{
|
||||||
|
vector<int> tmp_left = inorderTraversal(root->left);
|
||||||
|
vector<int> tmp_right = inorderTraversal(root->right);
|
||||||
|
tmp.insert(tmp.end(), tmp_left.begin(), tmp_left.end());
|
||||||
|
tmp.push_back(root->val);
|
||||||
|
tmp.insert(tmp.end(), tmp_right.begin(), tmp_right.end());
|
||||||
|
}
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Solution::test(TreeNode *root, vector<int> answer) {
|
||||||
|
return inorderTraversal(root) == answer;
|
||||||
|
}
|
||||||
19
94-binary-tree-inorder-traversal/soln.hpp
Normal file
19
94-binary-tree-inorder-traversal/soln.hpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct TreeNode
|
||||||
|
{
|
||||||
|
int val;
|
||||||
|
TreeNode *left;
|
||||||
|
TreeNode *right;
|
||||||
|
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||||
|
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||||
|
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||||
|
};
|
||||||
|
class Solution
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
vector<int> inorderTraversal(TreeNode *root);
|
||||||
|
bool test(TreeNode *root, vector<int> answer);
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user