题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2] Follow up:…
94. Binary Tree Inorder Traversal    二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack…
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]. 本题如果用recursive的方法非常简单.这里主要考察用iterative的方法求解.例如: [1,3,4,6,7,8,10,13,14] 从8开始依次将8,3,1 push入栈.这时root=None,每当roo…
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? 求二叉树的中序遍历,要求不是用递归. 先用递归做一下,很简单. /** * Defi…
题目描述: 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]. 解题思路: 使用栈.从根节点开始迭代循环访问,将节点入栈,并循环将左子树入栈.如果当前节点为空,则弹出栈顶节点,也就是当前节点的父节点,并将父节点的值加入到list中,然后选择右节点作为循环的节点,依次循环.…
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为空,则返回…
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Output: [,,] Follow up: Recursive solution is trivial, could you do it iteratively? 题目中要求使用迭代用法,利用栈的“先进后出”特性来实现中序遍历. 解法一:(迭代)将根节点压入栈,当其左子树存在时,一直将其左子树压入栈,…
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2] Follow up: Recursive solution is trivial, could you do it iteratively? 题意: 二叉树中序遍历 Solution1:   Recursion code class Soluti…
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? > r…
二叉树遍历(前序.中序.后序.层次.深度优先.广度优先遍历) 描述 解析 递归方案 很简单,先左孩子,输出根,再右孩子. 非递归方案 因为访问左孩子后要访问右孩子,所以需要栈这样的数据结构. 1.指针指向根,根入栈,指针指向左孩子.把左孩子当作子树的根,继续前面的操作. 2.如果某个节点的左孩子不存在,节点出栈,指针指向节点的右孩子.把这个右节点当作根, 继续前面的操作. 代码 /** * Definition for a binary tree node. * public class Tre…