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 /**
 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> inorderTraversal(TreeNode* root) {
13         if(!root) return ret;
14         tranverse(root);
15         return ret;
16     }
17
18     void tranverse(TreeNode * root)
19     {
20         if(!root) return;
21         tranverse(root->left);
22         ret.push_back(root->val);
23         tranverse(root->right);
24     }
25 private:
26     vector<int> ret;
27 };

迭代:

 1 class Solution {
 2 public:
 3     vector<int> inorderTraversal(TreeNode* root) {
 4         vector<int> ret;
 5         if(!root) return ret;
 6         map<TreeNode *, bool> m;
 7         stack<TreeNode *> s;
 8         s.push(root);
 9         while(!s.empty()){
10             TreeNode * t = s.top();
11             if(t->left && !m[t->left]){
12                 m[t->left] = true;
13                 s.push(t->left);
14                 t = t->left;
15                 continue;
16             }
17             ret.push_back(t->val);
18             s.pop();
19             if(t->right && !m[t->right]){
20                 m[t->right] = true;
21                 s.push(t->right);
22                 t = t->right;
23             }
24         }
25         return ret;
26     }
27 };
时间: 2024-10-07 08:43:19

LeetCode OJ: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,null,2,3], 1 2 / 3 return [1,3,2]. Note: Recursive(递归) solution is trivial, could you do it iteratively(迭代)? 思路: 解法一:用递归方法很简单, (1)如果root为空,则返回NU

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

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

[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

[C++]LeetCode: 95 Binary Tree Preorder Traversal (先序遍历)

题目: 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? Answer 1: 递归法 思路:如果当前节点不为空,先把当前节点的值放入数组.从

144. Binary Tree Preorder Traversal 先序遍历二叉树

Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree [1,null,2,3], 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? # Definition for a binary tree node. # cla

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 -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:Construct Binary Tree 前序和中序、后序和中序构建二叉树

前序和中序构建二叉树 后序和中序构建二叉树 分析:主要思路就是 在中序中找根节点然后划分左右子树,具体如下: 1. 查找根节点. 我们知道前序序列的第一个元素 和 后序序列的最后一个元素 肯定是根节点,我们就以此为突破口 2. 确定根节点的坐标. 我们在 中序序列中找到 根节点 的下标. 3. 分割左右子树. 对于中序序列:根节点下标之前的都属于左子树,之后的都属于右子树 对于先序序列:根节点之后的 一段元素(该段长度可以由中序序列的左子树长度确定)属于左子树,左子树之后的元素属于右子树 对于先

[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.