Given a binary tree, return the inorder traversal of its nodes ' values.
For example:
Given binary Tree {1,#,2,3},
return [1,3,2].
Note:recursive solution is trivial, could do it iteratively?
Binary tree in the middle sequence traversal non-recursive implementation, SAD is only know the need to use the stack to implement, but the specific code how to write but forget. The function call is implemented by the stack, so the middle sequence traversal, which is implemented by recursion, can be switched to the non-recursive implementation.
The specific analysis can refer to this blog.
Runtime:0ms
/** * Definition for a binary tree node. * struct TreeNode {* int val; * TreeNode *left; * TreeNode *righ T * TreeNode (int x): Val (x), left (null), right (NULL) {} *}; */classSolution { Public: vector<int>Inordertraversal (treenode* root) { vector<int>Resif(Root==null)returnRes TreeNode *node=root; Stack<TreeNode*>S while(!s.empty () | | node) { while(node) {S.push (node); node=node->left; } node=s.top (); S.pop (); Res.push_back (Node->val); node=node->right; }returnRes }};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode94:binary Tree inorder Traversal