首页 > 其他 > 详细

lintcode 二叉树中序遍历

时间:2017-08-18 19:13:49      阅读:233      评论:0      收藏:0      [点我收藏+]
 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13  //递归方法
14 class Solution {
15     /**
16      * @param root: The root of binary tree.
17      * @return: Inorder in vector which contains node values.
18      */
19 public:
20 
21     void inorder(TreeNode *root, vector<int> &result) {
22         if (root->left != NULL) {
23             inorder(root->left, result);
24         }
25         
26         result.push_back(root->val);
27         
28         if (root->right != NULL) {
29             inorder(root->right, result);
30         }
31         
32     }
33     
34     vector<int> inorderTraversal(TreeNode *root) {
35         // write your code here
36         vector<int> result;
37         if (root == NULL) 
38             return result;
39         inorder(root, result);
40         return result;
41     }
42 };

 

lintcode 二叉树中序遍历

原文:http://www.cnblogs.com/gousheng/p/7391246.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!