In-order traversal of binary tree (C/C++/JAVA)

Given the root node root of a binary tree, return its inorder traversal.
Example 1:
Insert image description here

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

Example 2:

Input: root = []
Output: []

Example 3:

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

Example 4:

Insert image description here

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

Example 5:

Insert image description here

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

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

The main difficulty of this problem is in-order traversal. Regarding the solution to this problem, I completed it recursively or iteratively and implemented it in different languages.
Problem-solving idea: Starting from the root node, continuously traverse the left subtree of the current node and add the node value to the answer, and then call the right subtree recursively until an empty node is encountered.

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 List<Integer> inorderTraversal(TreeNode root) {
    
    
        //定义

        List<Integer>list = new ArrayList();
        Stack<TreeNode> stack = new Stack<>();

        while(root!=null || !stack.isEmpty()){
    
    
            while(root!=null){
    
    
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            list.add(root.val);
            root=root.right;
        }
        return list;

    }
}

C language

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
void inorder(struct TreeNode *root, int * res, int *resSize){
    
    
    if(!root){
    
    
        return;
    }
    inorder(root->left,res,resSize);
    res[(*resSize)++] = root->val;
    inorder(root->right,res,resSize);
}

int* inorderTraversal(struct TreeNode* root, int* returnSize){
    
    
    int *res = malloc(sizeof(int)*501);
    *returnSize = 0;
    inorder(root,res,returnSize);
    return res;
}

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> res;
        inorder(root,res);
        return res;
    }
    void inorder(TreeNode* root ,vector<int>& res){
    
    
        if(!root){
    
    
            return;
        }
        inorder(root->left,res);
        res.push_back(root->val);
        inorder(root->right,res);
    }
};

Guess you like

Origin blog.csdn.net/smile66688/article/details/120335214