C++ — 迭代法遍历二叉树(LeetCode.94 - Binary Tree Inorder Traversal)

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;
}

  

原文地址:https://www.cnblogs.com/darkchii/p/8576029.html

时间: 2024-10-15 12:33:50

C++ — 迭代法遍历二叉树(LeetCode.94 - Binary Tree Inorder Traversal)的相关文章

LeetCode 94 Binary Tree Inorder Traversal (中序遍历二叉树)

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? 题目链接:https://leetcode.com/problems/binary-t

leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法

Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? confused what "{1,#,2,3}" means? > rea

LeetCode 94 Binary Tree Inorder Traversal(二叉树的中序遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其中序遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [1, 3, 2] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursi

Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)

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(迭代)? 思路: 解法一:用递归方法很简单, (1)如果root为空,则返回NU

leetcode 94.Binary Tree Inorder Traversal 二叉树的中序遍历

递归算法C++代码: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> in

Leetcode 94 Binary Tree Inorder Traversal 二叉树

二叉树的中序遍历,即左子树,根, 右子树 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 void dfs(vect

leetcode 94 Binary Tree Inorder Traversal ----- java

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? 求二叉树的中序遍历,要求不是用递归. 先用递归做一下,很简单. /** * Defini

Leetcode #94 Binary Tree Inorder Traversal

题目链接:https://leetcode.com/problems/binary-tree-inorder-traversal/ (非递归实现)二叉树的中序遍历. 1 class Solution 2 { 3 public: 4 vector<int> inorderTraversal(TreeNode *root) 5 { 6 vector<int> output; 7 if(root == NULL) 8 { 9 return output; 10 } 11 12 stack

94. Binary Tree Inorder Traversal 做题报告

题目链接: 94. Binary Tree Inorder Traversal 题目大意: 二叉树的中序遍历 做题报告: (1)该题涉及的算法,数据结构以及相关知识点 递归 (2)自己的解答思路+代码+分析时间和空间复杂度 递归思路 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) {