标签:example nod des solution cto blog root bsp turn
Given a binary tree, return the inordertraversal of its nodes‘ values.
For example:
Given binary tree{1,#,2,3},
1 2 / 3
return[1,3,2].
1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> inorderTraversal(TreeNode *root) { 13 vector<int> res; 14 if(root==NULL) return res; 15 stack<TreeNode*> s; 16 TreeNode* cur=root; 17 while(cur||!s.empty()){ 18 while(cur!=NULL){ 19 s.push(cur); 20 cur=cur->left; 21 } 22 cur=s.top(); 23 s.pop(); 24 res.push_back(cur->val); 25 cur=cur->right; 26 } 27 return res; 28 } 29 30 };
binary-tree-inorder-traversal——二叉树中序遍历
标签:example nod des solution cto blog root bsp turn
原文地址:http://www.cnblogs.com/zl1991/p/7056694.html