binary-tree-inorder-traversal——二叉树中序遍历

Given a binary tree, return the inordertraversal of its nodes‘ values.

For example:
Given binary tree{1,#,2,3},

   1
         2
    /
   3

return[1,3,2].

 1 /**
 2  * Definition for binary tree
 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         vector<int> res;
14         if(root==NULL) return res;
15         stack<TreeNode*> s;
16         TreeNode* cur=root;
17         while(cur||!s.empty()){
18             while(cur!=NULL){
19                 s.push(cur);
20                 cur=cur->left;
21             }
22             cur=s.top();
23             s.pop();
24             res.push_back(cur->val);
25             cur=cur->right;
26         }
27         return res;
28     }
29
30 };
时间: 2024-10-24 00:55:36

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)如果栈不空,取栈

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 二叉树的中序遍历

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

lintcode 容易题:Binary Tree Inorder Traversal 二叉树的中序遍历

题目: 二叉树的中序遍历 给出一棵二叉树,返回其中序遍历 样例 给出二叉树 {1,#,2,3}, 1 2 / 3 返回 [1,3,2]. 挑战 你能使用非递归算法来实现么? 解题: 程序直接来源 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val

Leetcode 94 Binary Tree Inorder Traversal 二叉树

二叉树的中序遍历,即左子树,根, 右子树 1 /** 2 * Definition for binary tree 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 void dfs(vect

leetcode 94.Binary Tree Inorder Traversal 二叉树的中序遍历

递归算法C++代码: 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> in

lintcode 容易题:Binary Tree Preorder Traversal 二叉树的前序遍历

题目: 二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 2 / 3 返回 [1,2,3]. 挑战 你能使用非递归实现么? 解题: 通过递归实现,根节点->左节点->右节点 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(in

leetcode 144. Binary Tree Preorder Traversal 二叉树的前序遍历

前序遍历的递归解法: 方法一C++: 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<