LeetCode94 Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes‘ values. (Medium)

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?

分析:

太经典基础的算法问题了,但想写出一个无bug的非递归二叉树中序遍历也不是很容易。先看递归版本的代码:

 1 class Solution {
 2 private:
 3     vector<int> result;
 4     void helper(TreeNode* root) {
 5         if (root == nullptr) {
 6             return;
 7         }
 8         helper(root -> left);
 9         result.push_back(root -> val);
10         helper(root -> right);
11     }
12 public:
13     vector<int> inorderTraversal(TreeNode* root) {
14         helper(root);
15         return result;
16     }
17 };

再考虑非递归,其实就是对于每个节点,走到最左端,沿路径压栈。

到达最左端后以此返回,开始弹栈,对于每个弹出的元素,记录其value,并且走向其右节点重复上述过程(走到最左端...)。

直到栈内元素为空为止。

代码:

 1 class Solution {
 2 public:
 3     vector<int> inorderTraversal(TreeNode* root) {
 4         vector<int> result;
 5         stack<TreeNode*> s;
 6         TreeNode* p = root;
 7         while (p || !s.empty()) {
 8             while (p != nullptr) {
 9                 s.push(p);
10                 p = p -> left;
11             }
12             if (!s.empty()) {
13                 p = s.top();
14                 result.push_back(p -> val);
15                 s.pop();
16                 p = p -> right;
17             }
18         }
19         return result;
20     }
21 };
时间: 2024-08-12 04:38:04

LeetCode94 Binary Tree Inorder Traversal的相关文章

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

题目: 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]. 分析: 中根遍历二叉树,也就是说,对树中的不论什么一个节点,先訪问其左子树,再訪问根节点,最后訪问其右子树.通过迭代的方式实现中根遍历二叉树,须要借助栈. 在遍历二叉树前,须要做点准备工作:包装每个树节点.包装后的节

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

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

41: Binary Tree Inorder Traversal

/************************************************************************/            /*       41:  Binary Tree Inorder Traversal                               */            /************************************************************************/  

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? 先贴递归实现的吧 1 /** 2

LeetCode: 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    /   3return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? co

[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) //非递归的中序遍历(