zoukankan      html  css  js  c++  java
  • LintCode 二叉树的遍历 (非递归)

    前序:

    class Solution {
    public:
        /**
         * @param root: The root of binary tree.
         * @return: Preorder in vector which contains node values.
         */
        vector<int> preorderTraversal(TreeNode *root) {
            // write your code here
            stack<TreeNode*> s;
            vector<int> res;
            while (root!= nullptr || !s.empty()) {
                while (root != nullptr) {
                    res.push_back(root->val);
                    s.push(root);
                    root = root->left;
                }
                if (!s.empty()) {
                    root = s.top();
                    s.pop();
                    root = root->right;
                }
            }
            return res;
        }
    };

    中序:

    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
            stack<TreeNode *> s;
            vector<int> res;
            while (root!=nullptr || !s.empty()) {
                while (root != nullptr){
                    s.push(root);
                    root = root->left;
                }
                if (!s.empty()) {
                    root = s.top();
                    res.push_back(root->val);
                    s.pop();
                    root = root ->right;
                }
            }
            return res;
        }
    };



    兴许:

    /**
     * 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: Postorder in vector which contains node values.
         */
    public:
        vector<int> postorderTraversal(TreeNode *root) {
            // write your code here
            vector<int> res;
            stack<TreeNode*> s;
            TreeNode * cur;
            TreeNode *pre = nullptr;
            if (root == nullptr) {
                return res;
            }
            s.push(root);
            while (!s.empty()) {
                cur = s.top();
                if ((cur->left == nullptr && cur->right == nullptr) || (pre != nullptr && (pre==cur->left || pre == cur->right))) {
                    res.push_back(cur->val);
                    s.pop();
                    pre = cur;
            }
            else {
                if (cur->right != nullptr) {
                    s.push(cur->right);
                }
                if (cur->left != nullptr) {
                    s.push(cur->left);
                }
                }
            }
            return res;
        }
        
    };
    


  • 相关阅读:
    GCD实现多个定时器,完美避过NSTimer的三大缺陷(RunLoop、Thread、Leaks)
    iOS适配UIViewView/WKWebView,H5生成长图,仿微信进度条
    翻译jquery官方的插件制作方法
    javascript引用和赋值
    薯片公司真实JS面试题(乐视TV)
    caller、call、apply、callee的用法和意思
    常用javascript类型判断
    Git 常用命令笔记(不定期持续记录)
    sublime text2 emmet 安装
    hash"#"
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6957211.html
Copyright © 2011-2022 走看看