class Solution {
public:
    vector<int> V;
    void inOrder(TreeNode* node)
    {
        if (node != NULL)
        {
            if (node->left != NULL)
            {
                inOrder(node->left);
            }

            V.push_back(node->val);

            if (node->right != NULL)
            {
                inOrder(node->right);
            }
        }
    }
    vector<int> inorderTraversal(TreeNode* root) {
        inOrder(root);
        return V;
    }
};

二叉树的中序遍历。

 

补充python版本实现:

 1 class Solution:
 2     def __init__(self):
 3         self.lists = []
 4         
 5     def inOrder(self,root):
 6         if root != None:
 7             if root.left != None:
 8                 self.inorderTraversal(root.left)
 9             self.lists.append(root.val)
10             if root.right != None:
11                 self.inorderTraversal(root.right)
12         
13         
14     def inorderTraversal(self, root: 'TreeNode') -> 'List[int]':
15         self.inOrder(root)
16         return self.lists

 

posted on 2017-05-11 12:14  Sempron2800+  阅读(192)  评论(0编辑  收藏  举报