zoukankan      html  css  js  c++  java
  • 二叉树前序遍历

    #include <iostream>
    #include <vector>
    #include <stack>
    
    struct BinaryTreeNode{
        int val;
        BinaryTreeNode* left;
        BinaryTreeNode* right;
        BinaryTreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    };
    
    //递归法
    void traveral(BinaryTreeNode*cur, std::vector<int>&nums)
    {
        if (cur == nullptr)
        {
            std::cout << "cur is null" << std::endl;
            return;
        }
        else{
            std::cout << "cur node: " << cur->val << ", ";
            nums.push_back(cur->val);
            std::cout << "traveral left" << ", ";
            traveral(cur->left, nums);
            std::cout << "traveral right" << ", ";
            traveral(cur->right, nums);
        }
    }
    
    
    std::vector<int> PreorderTraversalRecursion(BinaryTreeNode* root)
    {
        std::vector<int> result;
        traveral(root, result);
        return result;
    }
    
    //迭代法
    std::vector<int> PreorderTraversalIterator(BinaryTreeNode* root)
    {
        std::stack<BinaryTreeNode*> st;
        std::vector<int> result;
        if (root == nullptr)
        {
            return result;
        }
    
        st.push(root);//root进栈
        while (!st.empty())
        {
            BinaryTreeNode* node = st.top();//
    
            st.pop();//弹出
            result.push_back(node->val);//当前节点值
            if (node->right)
            {
                st.push(node->right);//右节点
            }
            if (node->left)
            {
                st.push(node->left);//左节点
            }
        }
        return result;
    }
    
    int main()
    {
        BinaryTreeNode *nodeOne = new BinaryTreeNode(1);
        BinaryTreeNode *nodeTwo = new BinaryTreeNode(2);
        BinaryTreeNode*nodeThree = new BinaryTreeNode(3);
        BinaryTreeNode*nodeFour = new BinaryTreeNode(4);
        BinaryTreeNode*nodeFive = new BinaryTreeNode(5);
        BinaryTreeNode*nodeSix = new BinaryTreeNode(6);
    
        //       1
       //      2   3
      //      4 5
     //          6
    //1-2-4-5-6-3
        nodeOne->left = nodeTwo;
        nodeOne->right = nodeThree;
    
        nodeTwo->left = nodeFour;
        nodeTwo->right = nodeFive;
    
        nodeFive->right = nodeSix;
    
        std::vector<int>resultsRecursion;
        resultsRecursion = PreorderTraversalRecursion(nodeOne);
        for (std::vector<int>::iterator it = resultsRecursion.begin(); it != resultsRecursion.end(); it++)
        {
            std::cout << (*it) << ", ";
        }
    
        std::cout << std::endl<< "-------------------------------------" << std::endl;
        std::vector<int>resultsIteration;
        resultsIteration = PreorderTraversalIterator(nodeOne);
        for (std::vector<int>::iterator it = resultsIteration.begin(); it != resultsIteration.end(); it++)
        {
            std::cout << (*it) << ", ";
        }
    }
  • 相关阅读:
    bootstrap的验证和确认对话框
    Jquery.Datatables 基本创建方法
    DOM – (w3school)1.DOM 方法 + 2.DOM属性 + 3.DOM 元素
    Jquery.Datatables 基本设置的中文注解
    修改android studio中的avd sdk路径、avd sdk找不到的解决方案
    一个人至少是需要学习3种语言的
    Framework7:不会Objective-C,也能开发iOS7应用
    私人定制,十款最佳Node.js MVC框架
    PHP开发框架流行度排名:Laravel居首
    仅用移动开发服务:开发native应用
  • 原文地址:https://www.cnblogs.com/crazybird123/p/14873531.html
Copyright © 2011-2022 走看看