码迷,mamicode.com
首页 > 其他好文 > 详细

lintcode 二叉树中序遍历

时间:2017-08-18 20:12:17      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:style   tree   his   blog   rsa   cto   div   order   ntc   

 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 二叉树中序遍历

标签:style   tree   his   blog   rsa   cto   div   order   ntc   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!