当先锋百科网

首页 1 2 3 4 5 6 7

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

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

// Source : https://oj.leetcode.com/problems/binary-tree-inorder-traversal/  
// Author : Chao Zeng  
// Date   : 2014-12-19 
struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x),left(NULL),right(NULL){}
};

//递归方法
class Solution {
public:
	vector<int> path;
	void inorder(TreeNode *root){
		if (!root)
			return;
		inorder(root->left);
		path.push_back(root->val);		
		inorder(root->right);
	}
	vector<int> inorderTraversal(TreeNode *root) {
		inorder(root);
		return path;
	}
};