给出一棵二叉树,返回这棵树的中序遍历 例如: 给出的二叉树为{1,#,2,3}, 1 \ 2 3 返回[1,3,2]. 备注:递归的解法太没有新意了,你能用迭代的方法来解这道题吗?
示例1
输入
{1,#,2,3}
输出
[1,3,2]
加载中...
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * } */ public class Solution { /** * * @param root TreeNode类 * @return int整型ArrayList */ public ArrayList
inorderTraversal (TreeNode root) { // write code here } }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 * @return int整型vector */ vector
inorderTraversal(TreeNode* root) { // write code here } };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param root TreeNode类 # @return int整型一维数组 # class Solution: def inorderTraversal(self , root ): # write code here
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * * @param root TreeNode类 * @return int整型一维数组 */ function inorderTraversal( root ) { // write code here } module.exports = { inorderTraversal : inorderTraversal };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param root TreeNode类 # @return int整型一维数组 # class Solution: def inorderTraversal(self , root ): # write code here
package main import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * * @param root TreeNode类 * @return int整型一维数组 */ func inorderTraversal( root *TreeNode ) []int { // write code here }
{1,#,2,3}
[1,3,2]