标签:
Given a binary tree, return the inorder traversal of its nodes‘ values.
和preorder是一样的,把左子节点一直压入到栈中,直到没有左子节点为止,然后出栈访问,存储右子节点。
1 vector<int> inorderTraversal(TreeNode *root) 2 { 3 vector<int> val; 4 stack<TreeNode *> path; 5 TreeNode *p = root; 6 while (!path.empty() || p != NULL) 7 { 8 while (p != NULL) 9 { 10 path.push(p); 11 p = p->left; 12 } 13 14 if (!path.empty()) 15 { 16 p = path.top(); 17 path.pop(); 18 19 val.push_back(p->val); 20 21 p = p->right; 22 } 23 } ; 24 25 return val; 26 }
[Leetcode]Binary Tree Inorder Traversal
标签:
原文地址:http://www.cnblogs.com/desp/p/4338620.html