leetCode之二叉树数中序遍历(递归实现)

1、题目描述

2、分析

对于树来说,由于其结构是递归定义的,所以对二叉树很多算法使用递归是最容易的。反倒是使用循环方式需要借助特殊的数据结构来实现。

3、代码

 1 vector<int> inorderTraversal(TreeNode* root) {
 2         vector<int> ans;
 3         if( root  == NULL )
 4             return ans;
 5         
 6         inorder(root,ans);
 7         return ans;
 8     }
 9     
10     void inorder(TreeNode* root, vector<int> & v)
11     {
12         if( root->left != NULL )
13             inorder( root->left , v);
14         v.push_back( root->val );
15         if( root->right != NULL )
16             inorder( root->right, v);
17     }
pp
原文地址:https://www.cnblogs.com/wangxiaoyong/p/8833093.html