|
| 1 | +--- |
| 2 | +id: binary-tree-inorder-traversal |
| 3 | +title: Binary Tree Inorder Traversal |
| 4 | +difficulty: Easy |
| 5 | +sidebar_label: 0094-Binary Tree Inorder Traversal |
| 6 | +tags: |
| 7 | + - Binary Tree |
| 8 | + - Inorder |
| 9 | + - Traversal |
| 10 | + - LeetCode Easy |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem |
| 14 | + |
| 15 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 16 | +| :---------------- | :------------ | :--------------- | |
| 17 | +| [Binary Tree Inorder Traversal ](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Binary Tree Inorder Traversal Solution on LeetCode](https://leetcode.com/problems/solutions/) | [Khushi Kalra](https://leetcode.com/u/abckhush/) | |
| 18 | + |
| 19 | +## Problem Description |
| 20 | +Given the root of a binary tree, return the inorder traversal of its nodes' values. |
| 21 | +### Example |
| 22 | +**Example 1:** |
| 23 | +```plaintext |
| 24 | +Input: root = [1,null,2,3] |
| 25 | +Output: [1,3,2] |
| 26 | +``` |
| 27 | +**Example 2:** |
| 28 | +```plaintext |
| 29 | +Input: root = [] |
| 30 | +Output: [] |
| 31 | +``` |
| 32 | +**Example 3:** |
| 33 | +```plaintext |
| 34 | +Input: root = [1] |
| 35 | +Output: [1] |
| 36 | +``` |
| 37 | + |
| 38 | +### Constraints |
| 39 | +1. The number of nodes in the tree is in the range `[0, 100]`. |
| 40 | +2. `-100 <= Node.val <= 100` |
| 41 | + |
| 42 | +## Approach: Recursion |
| 43 | +The problem is to perform an inorder traversal of a binary tree and return the values in a list. An inorder traversal visits the nodes of the binary tree in the following order: left subtree, root node, right subtree. |
| 44 | + |
| 45 | +We use a helper function traversal to perform the inorder traversal recursively. The main function inorderTraversal initializes the result list and calls the helper function. |
| 46 | + |
| 47 | +1. Initialization: |
| 48 | +- Create an empty vector inOrder to store the result of the inorder traversal. |
| 49 | + |
| 50 | +2. Recursive Traversal: |
| 51 | +- Define a helper function traversal that takes a TreeNode* and a reference to the inOrder vector. |
| 52 | +- If the current node (root) is NULL, return immediately (base case). |
| 53 | +- Recursively call traversal on the left subtree (root->left). |
| 54 | +- Push the value of the current node (root->val) to the inOrder vector. |
| 55 | +- Recursively call traversal on the right subtree (root->right). |
| 56 | + |
| 57 | +3. Main Function: |
| 58 | +- In the inorderTraversal function, create an empty vector inOrder. |
| 59 | +- Call the traversal function with the root of the binary tree and the inOrder vector. |
| 60 | +- Return the inOrder vector containing the inorder traversal result. |
| 61 | + |
| 62 | +## Complexity |
| 63 | +1. Time Complexity : $$O(h)$$ |
| 64 | +2. Space Complexity : $$O(n)$$ |
| 65 | + |
| 66 | +## Code |
| 67 | + |
| 68 | +### C++ |
| 69 | +```cpp |
| 70 | +/** |
| 71 | + * Definition for a binary tree node. |
| 72 | + * struct TreeNode { |
| 73 | + * int val; |
| 74 | + * TreeNode *left; |
| 75 | + * TreeNode *right; |
| 76 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 77 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 78 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 79 | + * }; |
| 80 | + */ |
| 81 | +class Solution { |
| 82 | +public: |
| 83 | + void traversal(TreeNode* root, vector<int>&inOrder){ |
| 84 | + if(root==NULL) return; |
| 85 | + traversal(root->left, inOrder); |
| 86 | + inOrder.push_back(root->val); |
| 87 | + traversal(root->right, inOrder); |
| 88 | + } |
| 89 | + vector<int> inorderTraversal(TreeNode* root) { |
| 90 | + vector<int>inOrder; |
| 91 | + traversal(root, inOrder); |
| 92 | + return inOrder; |
| 93 | + } |
| 94 | +}; |
| 95 | +``` |
| 96 | +
|
| 97 | +### Python |
| 98 | +```python |
| 99 | +# Definition for a binary tree node. |
| 100 | +class TreeNode: |
| 101 | + def __init__(self, val=0, left=None, right=None): |
| 102 | + self.val = val |
| 103 | + self.left = left |
| 104 | + self.right = right |
| 105 | +
|
| 106 | +class Solution: |
| 107 | + def traversal(self, root, inOrder): |
| 108 | + if root is None: |
| 109 | + return |
| 110 | + self.traversal(root.left, inOrder) |
| 111 | + inOrder.append(root.val) |
| 112 | + self.traversal(root.right, inOrder) |
| 113 | + |
| 114 | + def inorderTraversal(self, root): |
| 115 | + inOrder = [] |
| 116 | + self.traversal(root, inOrder) |
| 117 | + return inOrder |
| 118 | +``` |
0 commit comments