leetcode[96] Binary Tree Inorder Traversal

给定树根root。实现中序遍历,也就是左根右。

用递归的话,很简单,左边的返回值加上root的再加上右边的就行。

我自己写的有点挫:

/**
 * Definition for binary tree
 * 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> lf, ri, ans;
        if (root == NULL) return ans;
        lf = inorderTraversal(root -> left);
        ri = inorderTraversal(root -> right);
        lf.push_back(root -> val);
        for (int i = 0; i < ri.size(); ++i)
        {
            lf.push_back(ri[i]);
        }
        return lf;
    }
};

其实可以写简单一些

class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {
        vector<int> vi;
        inHelper(root, vi);
        return vi;
    }

    void inHelper(TreeNode *node, vector<int>& vi)
    {
        if(node == nullptr) return;
        inHelper(node->left, vi);
        vi.push_back(node->val);
        inHelper(node->right, vi);
    }
};

题目要求如果不用递归的话,用如下leetcode上的,利用堆,很妙。

vector<int> inorderTraversal(TreeNode *root)
    {
        vector<int> rs;
        if (!root) return rs;
        stack<TreeNode *> stk;
        TreeNode *p = root;
        while (!stk.empty() || p)
        {
            if (p)
            {
                stk.push(p);
                p = p->left;
            }
            else
            {
                p = stk.top();
                stk.pop();
                rs.push_back(p->val);
                p = p->right;
            }
        }
        return rs;
    }
时间: 2024-12-17 12:53:30

leetcode[96] Binary Tree Inorder Traversal的相关文章

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  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? 分析:求二叉树的中序

Leetcode 树 Binary Tree Inorder Traversal

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Inorder Traversal Total Accepted: 16984 Total Submissions: 48800 Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 /

[LeetCode][JavaScript]Binary Tree Inorder Traversal

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://leetcode.

【LeetCode】Binary Tree Inorder Traversal (2 solutions)

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? 解法一:递归 /** * Defi

LeetCode:Binary Tree Inorder Traversal

题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: 1 struct TreeNode { 2 int val; 3 TreeNode* left; 4 TreeNode* right; 5 TreeNode(int x): val(x), left(NULL),right(NULL) {} 6 }; 7 8 vector<int> preorderTraversal(TreeNode *root) //非递归的中序遍历(

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 No94. Binary Tree Inorder Traversal

Question: 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? 中序遍历,左-根-右 Algorithm: 递归,和非递归两种方法

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(二叉树中序遍历) 解题思路和方法

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