zoukankan      html  css  js  c++  java
  • [LeetCode]144.Binary Tree Preorder Traversal

    【主题】

    Given a binary tree, return the preorder traversal of its nodes' values.

    For example:
    Given binary tree {1,#,2,3},

       1
        
         2
        /
       3
    

    return [1,2,3].

    Note: Recursive solution is trivial, could you do it iteratively?

    【代码一】

    /*********************************
    *   日期:2014-10-15
    *   作者:SJF0115
    *   题号: 144.Binary Tree Preorder Traversal
    *   来源:https://oj.leetcode.com/problems/binary-tree-preorder-traversal/
    *   结果:AC
    *   来源:LeetCode
    *   总结:
    **********************************/
    #include <iostream>
    #include <malloc.h>
    #include <vector>
    using namespace std;
    
    struct TreeNode {
        int val;
        TreeNode *left;
        TreeNode *right;
        TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    };
    
    class Solution {
    public:
        vector<int> v;
        void PreOrder(TreeNode *root){
            if (root == NULL){
                return;
            }
            v.push_back(root->val);
            PreOrder(root->left);
            PreOrder(root->right);
        }
        vector<int> preorderTraversal(TreeNode *root) {
            PreOrder(root);
            return v;
        }
    };
    
    //按先序序列创建二叉树
    int CreateBTree(TreeNode* &T){
        char data;
        //按先序次序输入二叉树中结点的值(一个字符)。‘#’表示空树
        cin>>data;
        if(data == '#'){
            T = NULL;
        }
        else{
            T = (TreeNode*)malloc(sizeof(TreeNode));
            //生成根结点
            T->val = data-'0';
            //构造左子树
            CreateBTree(T->left);
            //构造右子树
            CreateBTree(T->right);
        }
        return 0;
    }
    
    int main() {
        Solution solution;
        TreeNode* root(0);
        CreateBTree(root);
        vector<int> v = solution.preorderTraversal(root);
        for(int i = 0;i < v.size();i++){
            cout<<v[i]<<endl;
        }
    }

    【代码二】

    非递归实现

    class Solution {
    public:
        vector<int> preorderTraversal(TreeNode *root) {
            vector<int> v;
            stack<TreeNode*> s;
            TreeNode* p = root;
            //栈不空或者p不空时循环
            while(p != NULL || !s.empty()){
                if(p != NULL){
                    //訪问根节点
                    v.push_back(p->val);
                    //根节点插入栈中,用来訪问右子树
                    s.push(p);
                    //遍历左子树
                    p = p->left;
                }
                else{
                    //左子树訪问完成。訪问右子树
                    p = s.top();
                    s.pop();
                    p = p->right;
                }
            }
            return v;
        }
    };


    【代码三】

    /*------------------------------------------------
    *   日期:2015-03-25
    *   作者:SJF0115
    *   题目: 144.Binary Tree Preorder Traversal
    *   来源:https://oj.leetcode.com/problems/binary-tree-preorder-traversal/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ------------------------------------------------------*/
    #include <iostream>
    #include <stack>
    #include <vector>
    using namespace std;
    
    // 二叉树节点结构
    struct TreeNode{
        int val;
        TreeNode *left;
        TreeNode *right;
        TreeNode(int x):val(x),left(nullptr),right(nullptr){}
    };
    
    class Solution {
    public:
        vector<int> preorderTraversal(TreeNode *root) {
            vector<int> result;
            if(root == nullptr){
                return result;
            }//if
            stack<TreeNode*> s;
            s.push(root);
            TreeNode *node;
            while(!s.empty()){
                node = s.top();
                s.pop();
                result.push_back(node->val);
                // 右子树
                if(node->right){
                    s.push(node->right);
                }//if
                // 左子树
                if(node->left){
                    s.push(node->left);
                }//if
            }//while
            return result;
        }
    };
    // 1.创建二叉树
    void CreateTree(TreeNode* &root){
        int val;
        //按先序次序输入二叉树中结点的值,‘-1’表示空树
        cin>>val;
        // 空节点
        if(val == -1){
            root = nullptr;
            return;
        }//if
        root = new TreeNode(val);
        //构造左子树
        CreateTree(root->left);
        //构造右子树
        CreateTree(root->right);
    }
    int main() {
        freopen("C:\Users\Administrator\Desktop\c++.txt", "r", stdin);
        TreeNode* root = nullptr;
        vector<int> result;
        // 创建二叉树
        CreateTree(root);
    
        Solution solution;
        result = solution.preorderTraversal(root);
        for(int i = 0;i < result.size();++i){
            cout<<result[i]<<" ";
        }
        cout<<endl;
        return 0;
    }
    






  • 相关阅读:
    tensorflow 查看模型输入输出saved_model_cli show --dir ./xxxx --all
    tensorflow models api:ValueError: Tensor conversion requested dtype string for Tensor with dtype float32: 'Tensor("arg0:0", shape=(), dtype=float32, device=/device:CPU:0)'
    python,ModuleNotFoundError,is not a package
    在docker集群下,使用VNC,物理机器重启后VNC失败解决
    2、闵氏空间
    1、爱因斯相对论(狭义相对论)
    tensorflow object detection api graph rewriter
    tensorflow_目标识别object_detection_api,RuntimeError: main thread is not in main loop,fig = plt.figure(frameon=False)_tkinter.TclError: no display name and no $DISPLAY environment variable
    目标检测识别在排除标注不标准问题下,为什么得分最高的框,不一定是最准的框.
    JS中各种变量类型在条件判断为false的情况
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5049062.html
Copyright © 2011-2022 走看看