Leetcode: 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?

递归解法(C++):

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution
{
private:
    vector<int> result;
public:
    vector<int> inorderTraversal(TreeNode *root)
    {
        if (root)
        {
            inorderTraversal(root->left);
            result.push_back(root->val);
            inorderTraversal(root->right);
        }
        return result;
    }
};

二叉树中序遍历的非递归算法和前序遍历思想差不多都是使用栈结构进行求解,参考Leetcode: Binary Tree Preorder Traversal(二叉树前序遍历)

非递归解法(C++):

class Solution
{
public:
    vector<int> inorderTraversal(TreeNode *root)
    {
        vector<int> result;
        stack<TreeNode*> nodes;
        TreeNode *node = root;
        while (node || !nodes.empty())
        {
            if (node)
            {
                nodes.push(node);
                node = node->left;
            }
            else
            {
                node = nodes.top();
                result.push_back(node->val);
                nodes.pop();
                node = node->right;
            }
        }
        return result;
    }
};

C#非递归解法:

/**
 * Definition for binary tree
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public class Solution
{
    public IList<int> InorderTraversal(TreeNode root)
    {
        IList<int> result = new List<int>();
        Stack<TreeNode> nodes = new Stack<TreeNode>();
        TreeNode node = root;
        //当result或者nodes不为空时
        while (node != null || nodes.Count > 0)
        {
            //node不为空的时候一直沿着左子树走,沿途节点入栈
            if (node != null)
            {
                nodes.Push(node);
                node = node.left;
            }
            //node空的时候,说明找到了最左子树
            else
            {
                //栈顶元素弹出放入result中
                node = nodes.Pop();
                result.Add(node.val);
                //node指向该节点的右子树,继续循环
                node = node.right;
            }
        }
        return result;
    }
}
时间: 2024-12-26 00:23:40

Leetcode: Binary Tree Inorder Traversal(二叉树中序遍历)的相关文章

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

题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: 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 Soluti

LeetCode OJ: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?中序遍历二叉树,递归遍历当然很容易,题目还要求不用递归,下面给出两种方法: 递归: 1 /**

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]. 题目要求对二叉树进行非递归的中序遍历,所谓前序遍历即,先访问左子树.再访问根节点.然后是右子树.通常采用递归的方法,题目要求采用非递归的方法实现.算法如下: 1)如果根节点非空,将根节点加入到栈中. 2)如果栈不空,取栈

[LeetCode] 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? > read

144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)

题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: 1 class Solution { 2 public: 3 vector<int> preorderTraversal(TreeNode* root) { 4 vector<int> ans; 5 if(root){ 6 TreeNode* temp; 7 stack<TreeNode*> s; //利用栈,

LeetCode: Binary Tree Inorder Traversal [094]

[题目] 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? >

[leetcode]Binary Tree Inorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ 题意:二叉树的中序遍历. 解题思路:这道题用递归解不难,所以应该考察的是非递归求解二叉树的中序遍历.我们使用一个栈来解决问题.比如一颗二叉树为{1,2,3,4,5,6,7},第一层为{1},第二层为{2,3},第三层为{4,5,6,7}.那么我们依次存储左子树的根节点,那么入栈顺序为:1,2,4.由于4的左子树为空,所以开始出栈.4出栈,检查4的右子树为空,继续

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

LeetCode Binary Tree Inorder Traversal

LeetCode解题之Binary Tree Inorder Traversal 原题 不用递归来实现树的中序遍历. 注意点: 无 例子: 输入: {1,#,2,3} 1 2 / 3 输出: [1,3,2] 解题思路 通过栈来实现,从根节点开始,不断寻找左节点,并把这些节点依次压入栈内,只有在该节点没有左节点或者它的左子树都已经遍历完成后,它才会从栈内弹出,这时候访问该节点,并它的右节点当做新的根节点一样不断遍历. AC源码 # Definition for a binary tree node