|
13 | 13 | Input : 40 10 5 1 -1 -1 -1 -1 30 -1 28 15 -1 -1 20 -1 -1
|
14 | 14 | Output : 1->5->10->40->30->15->28->20
|
15 | 15 | */
|
16 |
| -#include<bits/stdc++.h> |
17 |
| -using namespace std; |
18 |
| -class Node{ |
19 |
| - public: |
20 |
| - int data; |
21 |
| - Node* left; |
22 |
| - Node* right; |
23 |
| - |
24 |
| - Node(int x){ |
25 |
| - data = x; |
26 |
| - left = NULL; |
27 |
| - right = NULL; |
28 |
| - } |
| 16 | +#include <iostream> |
| 17 | +#include <vector> |
| 18 | +#include <stack> |
| 19 | + |
| 20 | +// Definition for a binary tree node. |
| 21 | +struct TreeNode { |
| 22 | + int val; |
| 23 | + TreeNode* left; |
| 24 | + TreeNode* right; |
| 25 | + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
29 | 26 | };
|
30 |
| -Node* build_binary_tree(){ |
31 |
| - int data; |
32 |
| - cin >> data; |
33 |
| - if(data == -1){ |
34 |
| - return NULL; |
| 27 | + |
| 28 | +class Solution { |
| 29 | +public: |
| 30 | + std::vector<int> inorderTraversal(TreeNode* root) { |
| 31 | + // Vector to store the in-order traversal result |
| 32 | + std::vector<int> result; |
| 33 | + |
| 34 | + // Stack to simulate the recursive call stack |
| 35 | + std::stack<TreeNode*> stack; |
| 36 | + |
| 37 | + // Current node starts from the root |
| 38 | + TreeNode* current = root; |
| 39 | + |
| 40 | + // Continue traversal until the current node is null and the stack is empty |
| 41 | + while (current != nullptr || !stack.empty()) { |
| 42 | + // Traverse all the way to the leftmost node, pushing each node onto the stack |
| 43 | + while (current != nullptr) { |
| 44 | + stack.push(current); |
| 45 | + current = current->left; |
| 46 | + } |
| 47 | + |
| 48 | + // Pop the top node from the stack (current leftmost node) |
| 49 | + current = stack.top(); |
| 50 | + stack.pop(); |
| 51 | + |
| 52 | + // Add the value of the current node to the result vector |
| 53 | + result.push_back(current->val); |
| 54 | + |
| 55 | + // Move to the right subtree of the current node |
| 56 | + current = current->right; |
| 57 | + } |
| 58 | + |
| 59 | + // Return the final in-order traversal result |
| 60 | + return result; |
35 | 61 | }
|
36 |
| - Node* root = new Node(data); |
37 |
| - root->left = build_binary_tree(); |
38 |
| - root->right = build_binary_tree(); |
39 |
| - return root; |
40 |
| -} |
41 |
| -void print_binary_tree(Node* root){ |
42 |
| - if(root == NULL){ |
43 |
| - return; |
| 62 | +}; |
| 63 | + |
| 64 | +// Example usage |
| 65 | +int main() { |
| 66 | + // Create a sample binary tree |
| 67 | + TreeNode* root = new TreeNode(1); |
| 68 | + root->right = new TreeNode(2); |
| 69 | + root->right->left = new TreeNode(3); |
| 70 | + |
| 71 | + // Perform in-order traversal |
| 72 | + Solution solution; |
| 73 | + std::vector<int> result = solution.inorderTraversal(root); |
| 74 | + |
| 75 | + // Print the result |
| 76 | + for (int val : result) { |
| 77 | + std::cout << val << " "; |
44 | 78 | }
|
45 |
| - print_binary_tree(root->left); |
46 |
| - cout << root->data << "->"; |
47 |
| - print_binary_tree(root->right); |
48 |
| -} |
49 |
| -int main(){ |
50 |
| - Node* root = build_binary_tree(); |
51 |
| - print_binary_tree(root); |
52 |
| - return 0; |
| 79 | + |
| 80 | + // Output: 1 3 2 |
| 81 | + return 0; |
53 | 82 | }
|
54 | 83 |
|
0 commit comments