94. Binary Tree Inorder Traversal

原创 2016年08月31日 11:14:30

题目

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?

分析

二叉树的中根遍历,最好写成迭代的。

递归法:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        if(root==NULL)
            return res;
        inorder(root->left,res);
        res.push_back(root->val);
        inorder(root->right,res);
        return res;
    }
    void inorder(TreeNode* r,vector<int>& v)
    {
        if(r==NULL)
            return;
        inorder(r->left,v);
        v.push_back(r->val);
        inorder(r->right,v);
    }
};
迭代法:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;//中根遍历结果向量
        stack<TreeNode*> inorder;//迭代法需要的栈
        if(root==NULL)
            return res;
        inorder.push(root);//先压入根节点
        TreeNode* r=root->left;//r指向左子树
        while(!inorder.empty())//如果栈非空
        {
            if(r!=NULL)//r不为NULL,则将r入栈,继续沿左子树遍历
            {
                inorder.push(r);
                r=r->left;
            }
            else//当r为NULL时
            {
                r=inorder.top();//弹出栈顶元素
                inorder.pop();
                res.push_back(r->val);//将值压入向量,进行中根遍历
                r=r->right;//指向右子树
                if(r!=NULL)//如果右子树不为NULL
                {
                    inorder.push(r);//将右子树入栈
                    r=r->left;//沿右子树的左子树继续遍历
                }
            }
        }
        return res;
        
    }
    
};



相关文章推荐

LeetCode94 Binary Tree Inorder Traversal(迭代实现) Java

题目: Given a binary tree, return the inorder traversal of its nodes’ values. For example: Given bi...

LeetCode 94. Binary Tree Inorder Traversal 题解

题目链接:点击打开链接   本题为非递归实现中序遍历。Java 参考代码如下: /** * Definition for a binary tree node. * public class ...

leetcode-94-Binary Tree Inorder Traversal

#include #include #include using namespace std; //Definition for a binary tree node. struct TreeN...

leetcode 94. Binary Tree Inorder Traversal-中序遍历|递归|非递归

原题链接:94. Binary Tree Inorder Traversal 【思路-Java、Python】 非递归实现用到了栈,以[1,2,3,4,5,null,6]为例,下图模拟了这个过程 ...

LeetCode 94 Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary t...

Leetcode 94:Binary Tree Inorder Traversal (二叉树中序遍历,非递归)

94. Binary Tree Inorder Traversal 提交地址: https://leetcode.com/problems/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 b...

Leetcode[94]-Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes’ values.For example: Given binary tr...

LeetCode 94: Binary Tree Inorder Traversal

Binary Tree Inorder TraversalGiven a binary tree, return the inorder traversal of its nodes’ values....

[leetcode]94. Binary Tree Inorder Traversal@Java解题报告

https://leetcode.com/problems/binary-tree-inorder-traversal/description/ Given a ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:94. Binary Tree Inorder Traversal
举报原因:
原因补充:

(最多只允许输入30个字)