【Leetcode】Binary Tree Inorder Traversal

263人阅读 评论(0) 收藏 举报
分类:

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

思路:

用栈模拟中序遍历过程

算法:

	public List<Integer> inorderTraversal(TreeNode root) {
		List<Integer> list = new ArrayList<Integer>();
		Stack<TreeNode> stack = new Stack<TreeNode>();
		while (root != null || !stack.isEmpty()) {
			if (root != null) {
				stack.push(root);
				root = root.left;  
			} else { //左子树为空,则访问val,且开始遍历右子树
				root = stack.pop();
				list.add(root.val);
				root = root.right;
			}
		}
		return list;
	}




查看评论

LeetCode 94 Binary Tree Inorder Traversal(Python详解及实现)

【题目】 Given a binary tree, return the inordertraversal of its nodes' values.   For example: Given bin...
  • yangjingjing9
  • yangjingjing9
  • 2017-08-10 16:10:57
  • 402

【LeetCode】【Python】Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes' values. For example: Given bin...
  • u011613729
  • u011613729
  • 2014-07-25 09:34:45
  • 2556

Binary Tree Inorder Traversal -- LeetCode

原题链接: http://oj.leetcode.com/problems/binary-tree-inorder-traversal/  通常,实现二叉树的遍历有两个常用的方法:一是用递归,二是使用...
  • linhuanmars
  • linhuanmars
  • 2014-03-01 05:38:48
  • 17134

【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...
  • crazy1235
  • crazy1235
  • 2016-06-01 23:42:08
  • 6336

【LeetCode】94. Binary Tree Inorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...
  • crazy1235
  • crazy1235
  • 2016-05-22 23:59:26
  • 8537

LeetCode 105:Construct Binary Tree from Preorder and Inorder Traversal

LeetCode 105: Given preorder and inorder traversal of a tree, construct the binary tree. 给定一个二叉树的...
  • sunao2002002
  • sunao2002002
  • 2015-05-20 23:09:41
  • 1128

leetcode笔记:Construct Binary Tree from Preorder and Inorder Traversal

这道题考察了先序和中序遍历,先序是先访问根节点,然后访问左子树,最后访问右子树;中序遍历是先遍历左子树,然后访问根节点,最后访问右子树。...
  • liyuefeilong
  • liyuefeilong
  • 2015-11-30 23:54:21
  • 1357

Construct Binary Tree from Inorder and Postorder Traversal -- LeetCode

原题链接: http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/  ...
  • linhuanmars
  • linhuanmars
  • 2014-04-24 09:02:17
  • 12095

C++详解Leetcode:105. Construct Binary Tree from Preorder and Inorder Traversal

原题思路通过二叉树的前序遍历和中序遍历来构建二叉树,通过递归可以很容易的解决这个问题,在遇到二叉树的问题,应该习惯先画图再来解决code/** * Definition for a binary t...
  • u014265347
  • u014265347
  • 2017-07-30 17:34:05
  • 364

[LeetCode-21]Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume ...
  • sbitswc
  • sbitswc
  • 2014-05-21 05:59:08
  • 3815
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 57万+
    积分: 8715
    排名: 2799
    博客专栏
    最新评论