Binary Tree Inorder Traversal – Leetcode Solution

In this post, we are going to solve the 94. Binary Tree Inorder Traversal problem of Leetcode. This problem 94. Binary Tree Inorder Traversal is a Leetcode easy level problem. Let’s see code, 94. Binary Tree Inorder Traversal – Leetcode Solution.

Problem

Given the root of a binary tree, return the inorder traversal of its nodes’ values.

Example 1 :

Input: root = [1,null,2,3]
Output: [1,3,2]

Example 2 :

Input: root = []
Output: []

Example 3 :

Input: root = [1]
Output: [1]

Constraints

  • The number of nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100

Now, let’s see the code of 94. Binary Tree Inorder Traversal – Leetcode Solution.

Binary Tree Inorder Traversal – Leetcode Solution

94. Binary Tree Inorder Traversal – Solution in Java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    
    public void inOrder(TreeNode root,List<Integer> list){
        if(root == null) return;
        
        inOrder(root.left,list);
        list.add(root.val);
        inOrder(root.right,list);
    }
    
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        inOrder(root,list);
        return list;
    }
}

94. Binary Tree Inorder Traversal – Solution in C++

/**
 * Definition for a binary tree node.
 * 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) {
        vector<int> nodes;
        inorder(root, nodes);
        return nodes;
    }
private:
    void inorder(TreeNode* root, vector<int>& nodes) {
        if (!root) {
            return;
        }
        inorder(root -> left, nodes);
        nodes.push_back(root -> val);
        inorder(root -> right, nodes);
    }
};

94. Binary Tree Inorder Traversal – Solution in Python

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def inorderTraversal(self, root):
        res = []
        self.helper(root, res)
        return res

    def helper(self, root, res):
        if root:
            self.helper(root.left, res)
            res.append(root.val)
            self.helper(root.right, res)
        

Note: This problem 94. Binary Tree Inorder Traversal is generated by Leetcode but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *