• LeetCode.94


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

    For example:
    Given binary tree [1,null,2,3],

       1
        
         2
        /
       3
    

    return [1,3,2].

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

      我的AC代码,递归写法比较简单,所以试试迭代:

    class Solution {
    public:
        vector<int> inorderTraversal(TreeNode* root) {
            vector<int> nums;
            stack<TreeNode *> stackNode;
            while(root)
            {
                stackNode.push(root);
                if (root->left != NULL)
                {
                    root = root->left;
                    continue;
                }
                nums.push_back(root->val);
                stackNode.pop();
                if (root->right != NULL)
                {
                    root = root->right;
                    continue;
                }
    
                TreeNode * node = root;
                if (!stackNode.empty())
                {
                    root = stackNode.top();
                    root->left = NULL;
                    stackNode.pop();
                }
    
                if (node == root)
                    break;
            }
            return nums;
        }
    };
    

      利用递归栈的思路,自己维护一个栈,时间复杂度应该是O(n),空间复杂度O(n)。

      看看AC后的详细细节:

      运行时间是3ms。

      不过我也写了一个递归版的:

    class Solution {
    public:
        void helper(TreeNode* root, vector<int> & nums) {
            if (root) {
                helper(root->left, nums);
                nums.push_back(root->val);
                helper(root->right, nums);
            }
        }
        
        vector<int> inorderTraversal(TreeNode* root) {
            vector<int> nums;
            helper(root, nums);
            return nums;
        }
    };
    

      这个运行结果感觉差不多:

      完整的迭代算法:

    #include <iostream>
    #include <vector>
    #include <stack>
    
    using namespace std;
    
    class Tree {
    public:
    	int val;
    	Tree * left;
    	Tree * right;
    	Tree() : val(), left(NULL), right(NULL) {}
    	void CreateTree(Tree * &root);
    	Tree* insert(Tree * root, int x);
    	void buildTree(Tree* root, vector<int> nums);
    	void inorderTraversal(Tree * root);
    	void iterative_inorderTraversal(Tree * root);
    };
    
    void Tree::CreateTree(Tree * &root)
    {
        int val;
        cin >> val;
        if (val == -1)
            root = NULL;
        else
        {
            root = new Tree;
            root->val = val;
            CreateTree(root->left);
            CreateTree(root->right);
        }
    }
    
    Tree* Tree::insert(Tree * root, int x)
    {
    	Tree * node = new Tree;
    	node->val = x;
    
    	if (root == NULL)
        {
            root = node;
            return root;
        }
    	else if (root->left == NULL)
        {
            root->left = node;
            return root;
        }
    	else if (root->right == NULL)
        {
            root->right = node;
            return root;
        }
        else
            node->left = root;
    
    	return node;
    }
    
    void Tree::buildTree(Tree* root, vector<int> nums)
    {
    	for (auto n : nums)
    		root = insert(root, n);
    }
    
    void Tree::inorderTraversal(Tree * root)
    {
    	if (root != NULL)
    	{
    		inorderTraversal(root->left);
    		cout << root->val << ' ';
    		inorderTraversal(root->right);
    	}
    }
    
    void Tree::iterative_inorderTraversal(Tree * root)
    {
        stack<Tree *> stackNode;
        while(root)
        {
            stackNode.push(root);
            if (root->left != NULL)
            {
                root = root->left;
                continue;
            }
            cout << root->val << ' ';
            stackNode.pop();
            if (root->right != NULL)
            {
                root = root->right;
                continue;
            }
    
            Tree * node = root;
            if (!stackNode.empty())
            {
                root = stackNode.top();
                root->left = NULL;
                stackNode.pop();
            }
    
            if (node == root)
                break;
        }
    }
    
    int main()
    {
    	//vector<int> nums{ 1,2,3 };
    	Tree * root = NULL;
    	root->CreateTree(root);
    
    	//root->buildTree(root, nums);
    	root->inorderTraversal(root);
    	cout << endl;
        root->iterative_inorderTraversal(root);
    
        return 0;
    }
    

      

  • 相关阅读:
    静态与动态数据分析的测试方法
    网址无法访问,显示服务器拒绝了请求
    stf浏览器端删除离线设备
    STF日志提示 Not found ; no service started
    通过adb命令获取apk的安装路径
    js页面打开方式
    windows和mac回退及撤销快捷键
    单元测试规范
    Git :fatal: refusing to merge unrelated histories解决
    mysql详解4:分组
  • 原文地址:https://www.cnblogs.com/darkchii/p/8576029.html
Copyright © 2020-2023  润新知