Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594

[LeetCode]Binary Tree Inorder Traversal-2014.01.18

Posted on 2014-01-18 02:44 Uriel 阅读(94) 评论(0)  编辑 收藏 引用 所属分类: LeetCode
裸的中序遍历二叉树,想着用来练记事本直接写代码能力来着,结果CE一次,敲错一个字母...囧

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<int> res;
13 
14     void inorder(TreeNode *root) {
15         if(root->left != NULL) inorder(root->left);
16         res.push_back(root->val);
17         if(root->right != NULL) inorder(root->right);
18     }
19     
20     vector<int> inorderTraversal(TreeNode *root) {
21         res.clear();
22         if(root == NULL) return res;
23         inorder(root);
24         return res;
25     }
26 };

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理