迭代 /*// Definition for a Node.class Node {public: int val; vector<Node*> children; Node() {} Node(int _val) { val = _val; } Node(int _val, vector<Node ...
分类:
其他好文 时间:
2020-03-16 12:44:47
阅读次数:
71
一:解题思路 这道题目2种做法。第一种做法就是递归法,第二种就是迭代法。这2种方法的时间复杂度和空间复杂度都为O(n)。 二:完整代码示例 (C++版和Java版) 递归C++: class Solution { public: void preorder(TreeNode* root, vecto ...
分类:
其他好文 时间:
2020-03-14 16:31:41
阅读次数:
46
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 s ...
分类:
其他好文 时间:
2020-03-08 11:13:53
阅读次数:
88
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, ...
分类:
其他好文 时间:
2020-03-01 12:41:23
阅读次数:
61
https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal/submissions/ 常规解法: 递归实现,迭代实现 111. Minimum Depth of Binary Tree class Solution { public ...
分类:
其他好文 时间:
2020-02-26 01:16:19
阅读次数:
91
题目 输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。 input : 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9,3,15,20,7] 思路 这道题我不会做,看了题解之后,才发现有迹可 ...
分类:
其他好文 时间:
2020-02-22 16:13:10
阅读次数:
77
38. 二叉树的深度 题目描述 输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。 1 class Solution { 2 public: 3 // 前序递归遍历,分别统计左右子树的高度 4 int preOrder(TreeN ...
分类:
其他好文 时间:
2020-02-19 23:41:34
阅读次数:
84
遍历一棵二叉树常用的有四种方法,前序(PreOrder)、中序(InOrder)、后序(PastOrder)还有层序(LevelOrder)。前中后序三种遍历方式都是以根节点相对于它的左右孩子的访问顺序定义的。例如根->左->右便是前序遍历,左->根->右便是中序遍历,左->右->根便是后序遍历。而 ...
分类:
其他好文 时间:
2020-02-19 19:17:37
阅读次数:
86
二叉树的递归遍历很容易写出来,对于递归遍历则需要借助辅助栈,并且不同的遍历次序迭代的写法也不尽相同,这里整理一些二叉树迭代遍历的实现 二叉树的前序遍历 [leetcode144]:https://leetcode cn.com/problems/binary tree preorder traver ...
分类:
其他好文 时间:
2020-02-11 00:35:37
阅读次数:
73
1 """ 2 For example, given 3 preorder = [3,9,20,15,7] 4 inorder = [9,3,15,20,7] 5 Return the following binary tree: 6 3 7 / \ 8 9 20 9 / \ 10 15 7 11 ...
分类:
其他好文 时间:
2020-02-02 23:34:00
阅读次数:
66