topic:
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3]
,
return [1,3,2]
.
Note: Recursive solution is trivial, could you do it iteratively?
Title:The in-order traversal of the binary tree, that is, output according to the left subtree of the node, the node, and the right subtree of the node, is implemented in a recursive way. A C++ code is implemented as follows:
#include<iostream> #include<cstdio> #include<cstdlib> /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ using namespace std; class Solution { public: vector<int> inorderTraversal(TreeNode* root) { vector<int> v; inorder(v,root); return v; } void inorder(vector<int>& v, TreeNode* t) { if(t == NULL) { return; } inorder(v,t->left); v.push_back(t->val); inorder(v,t->right); } };