题目描述 求给定的二叉树的前序遍历。 例如: 给定的二叉树为{1,#,2,3}, 1 \ 2 / 3 返回:[1,2,3]. 备注;用递归来解这道题太没有新意了,可以给出迭代的解法么? /** * struct TreeNode { * int val; * struct TreeNode *lef ...
分类:
其他好文 时间:
2020-08-05 13:09:09
阅读次数:
71
题干 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to ...
分类:
其他好文 时间:
2020-07-29 21:34:20
阅读次数:
77
根据BST的前序遍历重建BST 1. 平均O(NlogN) 最坏O(N^2) class Solution { public: TreeNode* dfs(int l, int r, vector<int>& p) { if (l > r) return nullptr; TreeNode* nod ...
分类:
其他好文 时间:
2020-07-23 16:13:09
阅读次数:
67
03-树3 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that w ...
分类:
其他好文 时间:
2020-07-19 23:08:36
阅读次数:
84
链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ 代码 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...
分类:
其他好文 时间:
2020-07-18 11:34:31
阅读次数:
66
题目链接:https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/ 方法一递归法:先访问子节点,然后访问根。LeetCode代码: /* // Definition for a Node. class Node { public ...
分类:
其他好文 时间:
2020-07-16 21:39:10
阅读次数:
79
题目链接 https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ 题解一:递归 // Problem: LeetCode 94 // URL: https://leetcode-cn.com/problems/binary-tr ...
分类:
其他好文 时间:
2020-07-13 15:36:49
阅读次数:
58
题目链接 https://leetcode-cn.com/problems/binary-tree-preorder-traversal/description/ 题解一:递归 // Problem: LeetCode 144 // URL: https://leetcode-cn.com/prob ...
分类:
其他好文 时间:
2020-07-13 00:00:50
阅读次数:
90
题目描述链接:https://leetcode-cn.com/problems/binary-tree-level-order-traversal/ 解题思路:参考官方题解,解题思路如下:对于二叉树的层序遍历,首先应该考虑到的数据结构便是队列,利用队列现进先出的特性,可以很方便的解决此题。 (1)根 ...
分类:
其他好文 时间:
2020-07-12 20:29:04
阅读次数:
65
Given an n-ary tree, return the level order traversal of its nodes' values. Nary-Tree input serialization is represented in their level order traversa ...
分类:
其他好文 时间:
2020-07-10 11:20:42
阅读次数:
63