leetcode || 94、Binary Tree Inorder Traversal

problem:

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 more on how binary tree is serialized on OJ.

Hide Tags

Tree Hash
Table
 Stack

题意:二叉树的中序遍历

thinking:

(1)二叉树的中序遍历,给出递归法和非递归法两种。

(2)递归法的思路是,先递归访问当前结点的左孩子,再打印当前结点的值,再递归访问当前结点的右孩子。

(3)非递归法是借助堆栈实现的,思路也是先将所有左孩子节点放入堆栈中,左孩子为NULL时,打印堆栈顶点结点,

出栈,访问其右孩子。如果其右孩子的左孩子不为NULL,重复上述过程。

code:

非递归法:

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

递归法:

class Solution {
private:
    vector<int> ret;
public:
    vector<int> inorderTraversal(TreeNode *root) {

            inorder(root);
            return ret;        

    }
protected:
    void inorder(TreeNode *node)
    {
        if(node!=NULL)
        {
            inorder(node->left);
            ret.push_back(node->val);
            inorder(node->right);
        }
    }
};
时间: 2024-09-30 13:57:39

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,#,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? 说明:1)下面有两种实现:递归(Recursive )与非递归(迭代iterative

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 || 144、Binary Tree Preorder Traversal

problem: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? Hide Tags Tree Stack 题意:非递归前序遍历二叉树 t

leetcode || 145、Binary Tree Postorder Traversal

problem: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? Hide Tags Tree Stack 题意:非递归后续遍历二叉树

LeetCode(94)Binary Tree Inorder Traversal

题目如下: Python代码: def inorderTraversal(self, root): res = [] self.helper(root, res) return res def helper(self, root, res): if root: self.helper(root.left, res) res.append(root.val) self.helper(root.right, res)

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

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

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) {