19.2.24 [LeetCode 94] Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes' values.

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3

Output: [1,3,2]
 1 class Solution {
 2 public:
 3     void build(TreeNode*root, vector<int>&ans) {
 4         if (root == NULL)return;
 5         build(root->left, ans);
 6         ans.push_back(root->val);
 7         build(root->right, ans);
 8     }
 9     vector<int> inorderTraversal(TreeNode* root) {
10         vector<int>ans;
11         build(root, ans);
12         return ans;
13     }
14 };
View Code

 

注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
posted @ 2019-02-24 21:39  TobicYAL  阅读(105)  评论(0编辑  收藏  举报