Binary Tree Inorder Traversal(C++二叉树的中序遍历)

解题思路:

(1)递归求解

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: A Tree
     * @return: Inorder in ArrayList which contains node values.
     */
    vector<int> v;
    vector<int> inorderTraversal(TreeNode * root) {
        // write your code here
        inorder(root);
        return v;
    }
    
    void inorder(TreeNode *root) {
        if (root) {
            inorder(root->left);
            v.push_back(root->val);
            inorder(root->right);
        }
    }
};
发布了317 篇原创文章 · 获赞 279 · 访问量 43万+

猜你喜欢

转载自blog.csdn.net/coolsunxu/article/details/105701203