求给定的二叉树的前序遍历。 例如: 给定的二叉树为{1,#,2,3}, 返回:[1,2,3]. 备注;用递归来解这道题很简单,你可以给出迭代的解法么? 如果你不明白{1,#,2,3}的含义,点击查看相关信息
示例1
输入
{1,#,2,3}
输出
[1,2,3]
加载中...
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
preorderTraversal (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
preorderTraversal(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 preorderTraversal(self , root ): # write code here
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * * @param root TreeNode类 * @return int整型一维数组 */ function preorderTraversal( root ) { // write code here } module.exports = { preorderTraversal : preorderTraversal };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param root TreeNode类 # @return int整型一维数组 # class Solution: def preorderTraversal(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 preorderTraversal( root *TreeNode ) []int { // write code here }
{1,#,2,3}
[1,2,3]