原题 Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. 分析 对二叉树进行先序遍历,即根节点->左子树->右子树 代码: 递归: 递归代码十分简单,建立一个vector作为返回的结果,现将根节点push进去,然后递归处理左子树和右子树. class Solution
[题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,2,3] [思路] 有参考,好机智,使用堆栈压入右子树,暂时存储. 左子树遍历完成后遍历右子树. [代码] /** * Definition for a binary tree node. * public class TreeNode { *
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree [1,null,2,3], 1 \ 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 递归 : class Solution { private List<Intege
/** * 实现二叉树的创建.前序遍历.中序遍历和后序遍历 **/ package DataStructure; /** * Copyright 2014 by Ruiqin Sun * All right reserved * created on 2014-9-9 下午2:34:15 **/ public class BinTreeInt { private Node root; /** * 创建内部节点类 **/ private class Node{ // 左节点 private Nod
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,2,3] Follow up: Recursive solution is trivial, could you do it iteratively? 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3
描述 给出一棵二叉树,返回其节点值的前序遍历. 您在真实的面试中是否遇到过这个题? 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. Binary Tree Preorder Traversal Description Given a binary tree, return the preorder traversal of its nodes' values. /** * Definition of TreeNode: * public class Tre