Binary Tree Inorder Traversal ——LeetCode

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?

题目大意:中序遍历一个二叉树,递归的方案太low,用迭代的方式来写?

解题思路:不用递归,那就自己实现栈呗

1、首先节点入栈,处理当前节点左孩子,并且压入栈,当左节点非空,循环遍历;

2、找到第一个左孩子为空的节点,将此节点出栈,将节点值加入结果链表,并把当前节点设为右孩子;

3、循环到栈为空。

 1     public List<Integer> inorderTraversal(TreeNode root) {
 2         List<Integer> res = new ArrayList<>();
 3         Stack<TreeNode> stack = new Stack<>();
 4         TreeNode curr = root;
 5         while (curr != null || !stack.isEmpty()) {
 6             while (curr != null) {
 7                 stack.add(curr);
 8                 curr = curr.left;
 9             }
10             curr = stack.pop();
11             res.add(curr.val);
12             curr = curr.right;
13         }
14         return res;
15     }
时间: 2024-10-09 18:06:59

Binary Tree Inorder Traversal ——LeetCode的相关文章

Binary Tree Inorder Traversal leetcode 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]. Note: Recursive solution is trivial, could you do it iteratively? 题解: 中序遍历:递归左 处理当前 递归右. 画图的话就是,之前离散老师教的,从ro

Binary Tree Inorder Traversal -- LeetCode 94

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]. Solution 1: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode

Binary Tree Inorder Traversal [leetcode] 非递归的三种解法

第一种方法是Morris Traversal 是O(n)时间复杂度,且不需要额外空间的方法.缺点是需要修改树. 通过将叶子节点的right指向其中序后继. 代码如下 vector<int> inorderTraversal(TreeNode *root) { vector<int> res; TreeNode * cur = root; TreeNode * pre = NULL; while (cur) { if (cur->left == NULL) { res.push

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

[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 Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

LeetCode: Binary Tree Inorder Traversal [094]

[题目] 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? >

[leetcode]Binary Tree Inorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ 题意:二叉树的中序遍历. 解题思路:这道题用递归解不难,所以应该考察的是非递归求解二叉树的中序遍历.我们使用一个栈来解决问题.比如一颗二叉树为{1,2,3,4,5,6,7},第一层为{1},第二层为{2,3},第三层为{4,5,6,7}.那么我们依次存储左子树的根节点,那么入栈顺序为:1,2,4.由于4的左子树为空,所以开始出栈.4出栈,检查4的右子树为空,继续