码迷,mamicode.com
首页 > 其他好文 > 详细

1.二叉树的中序遍历

时间:2017-08-06 18:03:33      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:span   tor   roo   nbsp   rsa   write   height   二叉树   .com   

题目:给出一棵二叉树,返回其中序遍历技术分享

/**

 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Inorder in vector which contains node values.
     */
public:
    vector<int> inorderTraversal(TreeNode *root) {
        // write your code here
        vector<TreeNode *> t;
        vector<int> res;
while(root != NULL || t.size() != 0) {
while(root != NULL) {
                t.push_back(root);
                root = root->left;
            }
            root = t.back();
            t.pop_back();
            res.push_back(root->val);
            root = root->right;
        }
        return res;
    }
};

1.二叉树的中序遍历

标签:span   tor   roo   nbsp   rsa   write   height   二叉树   .com   

原文地址:http://www.cnblogs.com/ALIMAI2002/p/7206740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!